Please help very confused


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


You have to put 'cobra' at index of duck_index, read theory again, As I remember we have to use that variable (duck_index) instead of manually putting 2.


animals.insert(duck_index, 'cobra')


The .index() function grabs the index of the list item "duck" and stores it in the variable duck_index.
Then the .insert() function takes the string "cobra" and inserts it in the original position of "duck" on the list, moving, but not replacing, "duck" to the next index.
The .insert() function would accept the variable duck_index as arguments as it already holds the index of "duck" as its value.

I'm learning too. I was stuck at this point, but I figured it out few moments ago, and thought I should help explain it to the best of my knowledge. Good luck


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