Pass duck_index as the first argument in insert() (error)


#1



animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")  

print animals.index("duck")

print animals.insert(2,"cobra")

print animals


#2

Are you still having a problem with your code?


#3

yes..!
Can u help me with it..?


#4

This should be the position of duck in your list not the string,

should be,

duck_index = (2)

so then this line,

can be,

animals.insert(duck_index, "cobra")


#5

Thanks for the help here. I tried a numerical digit in the animals.insert line, I tried "duck", I kept getting errors. Then as soon as I saw this answer I had a "duhhhhh" moment. You are setting "duck_index" to find the position of duck. So the animals.insert(duck_index, "cobra") line, you are just telling it to "check the duck_index line, yo, and replace it with cobra!" The lesson was a bit vague as to WHY we were doing what we were doing.


#6

Thanks for the help bandit :slight_smile:


#7

Correct and correct, haha :smile:


#8

You're welcome @courseplayer78813 :slight_smile:


#9

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