Maintaining Order


animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")   # Use index() to find "duck"
print duck_index
# Your code here!
animals.insert(2, "cobra")

print animals # Observe what prints after the insert operation

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

Solution :

animals.insert(duck_index, "cobra")

This exercise was kinda brainless timewaste for me, shorter code is always preferable when output is the same so I couldnt find it for a long time.


I do agree that it'd be more efficient to simply type the index instead of the variable, however this section of the course is teaching you how to use the list. You learn what .index() does in a way that is easy to teach and to understand, which is fine when people are beginning in Python.


Yeah i get it.All the python course here seems very fine, its just sometimes not foolproof enough for dumb guys like me.Maybe sometimes there should be an option "Ok i feel stupid, show me the answer", it could spare some priceless minutes when you cant debug yourself or you tried everything you can think of.


That used to be a thing - if you got it wrong enough times, a 'Get Code' option would come up.
I don't know if it's still around.


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