7. Maintaining Order


#1



https://www.codecademy.com/courses/python-beginner-en-pwmb1/1/4?curriculum_id=4f89dab3d788890003000096#


Oops, try again. Make sure to pass duck_index as the first argument in insert().


Replace this line with your code. 
animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")   # Use index() to find "duck"

# Your code here!
animals.insert(2, "cobra")


print animals # Observe what prints after the insert operation


#2

Hi @singh_singh,

In this statement ...

animals.insert(2, "cobra")

... instead of passing 2 as an argument, use the duck_index variable directly as the first argument in calling the insert method.


#3

Hello @singh_singh,

Check the below code.

animals.insert(duck_index, "cobra")
print animals

#4

why should we use duck_index instead of 2?


#5

@systemrunner76765 This is because you have to pretend as if you do not know the actual index of duck. If the list wasn't in front of you, you would need to have a code that would know. So the index is assigned to the variable "duck_index". This variable now holds that index. So if you use it instead of "2" in your code, your code will work no matter which index the "duck" is in. I hope that explanation helped!


#6

That worked, thanks!


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.