Maintaining order


#1

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]

Use index() to find "duck"

duck_index = animals.index("duck")
print duck_index

Your code here!

animals.insert(2, "cobra")

print animals # Observe what prints after the insert operation

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

what am I doing wrong??


#2

Write :

animals = animals.insert(3, "cobra")

instead of :


#3

it doesn't work because we have to replace the index value of ducks with cobra and the index value for ducks is "2"


#4

your insert function should take duck_index as first argument, duck_index holds the index of duck, i know that is the same as two, but the exercise wants you to use the index you just fetched to insert the new element


#5

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

Your code here!

animals.insert(3, "cobra")

print animals # Observe what prints after the insert operation

now the error is Oops, try again. The new item should have been inserted at the index previously occupied by "duck".

and is printing this to console
['aardvark', 'badger', 'duck', 'cobra', 'emu', 'fennec fox']
None


#6

you first argument inside insert:

animals.insert(3, "cobra")

is still a number isntead of duck_index. (you need to use the variable you created)


#7

now its clear to me and worked fine thank you !!