7. Maintaining Order



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


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.


Hello @singh_singh,

Check the below code.

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


why should we use duck_index instead of 2?


@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!


That worked, thanks!


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