7/14 Help


#1
 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

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


#2

I was having the same exact issue as the example clearly shows it using the numeric index, and I realized that the error is literally calling for "duck_index as the first argument in insert()"

so I changed: animals.insert(2, "cobra")

to: animals.insert(duck_index, "cobra")

and it worked. I'm not sure why it wouldn't work with the 2, hopefully someone can answer that.


#3

It should look something like this:

my_list = [1,9,3,8,5,7]

for number in my_list:
print number * 2


#4

Technically, it should work. But, I think due to the instructions they want you to create a new value == duck_index.


#5

I see. It makes sense now.


#6

The reason for calling duck_index is so that we understand how to create and input at the same time, if we were to have placed the "2" where we have "duck_index"

(Let's move duck from 3(2) to 5(4) for this example)
animals = ["aardvark", "badger", "fennec fox", "emu","duck"]
duck_index = animals.index("duck")

animals.insert(4, "cobra")

print animals

in that example, cobra will always show up in that fourth indiced placement(5th on our list)

but if we were to use - animals.insert(duck_index, "cobra")

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

animals.insert(duck_index, "cobra")

print animals

no matter where duck is placed in our list, our insert will locate the word "duck" and place "cobra" before it.

Hopefully I didn't drag that on too much, also hope it helped in some way.


#7

put speech marks around cobra. change 2 to duck_index :grinning:


#8

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

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