Maintaining Order


#1

Hey guys, I'm having some difficulties with this one. I have been stuck here for hours and I did everything the way its supposed to be but obviously I didn't because otherwise it would have worked. So, this is the error i keep getting 'Make sure to pass duck_index as the first argument in insert()." I have no idea what its trying to tell me also the directions were a little confusing "Use the .index(item) function to find the index of "duck". Assign that result to a variable called duck_index." I found the index of "duck" via "animals.index("duck") but I don't understand the second part. Assigning the result to the variable duck_index. I'm sure its something very simple and I'm probably overthinking it. I appreciate the help. Thank you in advance.


#2

I think it wants you to use

animals.insert(duck_index, "cobra")

instead of

animals.insert(2, "cobra")

#3

@codeninja84962
AHA!!! thank you. I assigned animals.index("duck") to the variable duck_index afterwards, I did what you told me to do and it worked but why make a variable duck_index when I could have just done this...

1.animals = ['aardvark', 'badger', 'duck', 'emu', 'fennec foc']
2.animals.index('duck')
3.print animals.index('duck')
4.
5. animals.insert(2, "cobra")
6.
7.print animals

would this have worked?


#4

Well, you still manipulate the animals-list in the same way, but the assignment is specifically to find out the index of "duck" and insert "cobra" there using .index(), .insert() and the 'duck_index' variable. Pretend that you can't actually see what's inside the animals-list and I think the exercise could make a bit more sense.


#5

Sorry I still couldn't figure it out, please help.


#6

this is what you have to do:
animals = ["a", "b", "c", "d"]
duck_index = animals.index("c")

animals.insert(duck_index, "cobra")
print animals

you should get a, b, cobra, c, d as the answer.

hope this helps


#7

The below code worked for me
animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck")
print duck_index
animals.insert(duck_index,"cobra")
print animals

hope this helps


#8

Tysm that helped, I've been through about twenty of these until I tried this