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().


I'm a bit lost here. First of all, I think the error message is incorrect, or I'm missing something major, and secondly, I can't get "cobra" to take the place of "duck"


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



print animals # Observe what prints after the insert operation


#2

in insert(), don't use a fixed number. Use the duck_index variable you declared on line 2


#3

It doesn't let me. I tried that. It says I need to use an integer.


#4

can i see this code then? I can't fix code i don't see


#5

Nevermind. solved this. However I am confused:

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

Your code here!

print animals # Observe what prints after the insert operation

This will not print the result. I get this as output:

None
None

???


#6

you are still not using duck_index

insert() will update the list for you, no need to also store it in a variable, doing this actually results in None


#7

When I don't store insert() in a variable, it comes up incorrect. That's how I got this:
Oops, try again. Make sure to pass duck_index as the first argument in insert().

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

print animals # Observe what prints after the insert operation


#8

because (for the third time), you need to use duck_index instead of a fixed number when inserting

we retrieve the index of duck, store it in a variable so we use this variable when inserting, this makes your program more flexible


#9

Oh, I understand now. I got an error message before stating that I had to use an integer( I had done animals.insert("duck,"cobra")) and I presumed that meant that I could not use letters in the code. My mistake.


#10

insert() requires an integer, "duck" is a string

duck_index is a variable containing a integer. Good you fixed it :slight_smile:


#11