7/14 - Maintaining Order


#1

I have this code:

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

Your code here!

duck_index=animals.index("duck")
print(duck_index)
animals.insert(2,"cobra")

print animals # Observe what prints after the insert operation

and i get the following message:

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

can somebody help me? thx


#2

I did the same mistake, English is not my prime language so maybe that why. :slightly_smiling:

" Make sure to pass duck_index as the first argument in insert()."

animals.insert(duck_index, "cobra") Try this.


#3

thank you very much, that was really helpfull :slight_smile:


#5

can someone explain why we do this???


#6

Could you specify? Do you mean, why this kind of structure is used?


#7

i don't really understand any of it
if someone can explain it line by line i would be very gratefull


#8

Ok I'll try to explain it line by line:

animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
-->animals (variable) = list of animals

duck_index = animals.index("duck") # Use index() to find "duck"
-->duck_index = so-called position of the word "duck" in the list
-->for example animals = ["monkey","bunny"], then animals.index("monkey") = 0 or animals.index("bunny") = 1

duck_index=animals.index("duck")
-->here you assign the the "position" of the word "duck" to duck_index, which is an integer
print(duck_index)
-->here it is printed
animals.insert(duck_index,"cobra")
-->at this line, the list "animals" got appended by a new word "cobra" at the position "2" which should actually be "duck index", which is the position you want the word "cobra" to be

I hope you understood all of it ^^

print animals # Observe what prints after the insert operation


#9

great explanation! i understood it


#10

Thank you, this helped


#11

Make sure cobra is lowercase.. took me 30 minutes of hating codeacademy to find out it i had Cobra instead...


#12

thnx , u helped me :heart_eyes:


#13

that helped! Thanks!


#14

Thank for the help, I had the same issue.


#15

This!

Cobra is a proper noun! needs to be capitalized!


#16

thanks that was very helpfull


#17

My code as below only lead to error message as:
"Oops, try again. It looks like "cobra" is not at the correct location (index 4)."
Please who give me a hand, thanks very much.


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

animals.insert(duck_index,"corba")

print animals # Observe what prints after the insert operation


#18

after modification as below it works. : )

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

animals=animals.insert(duck_index,"corba")

print animals # Observe what prints after the insert operation


#19

i really dont understand this lesson. Why on earth are you finding duck. and insering. its much easier just to change the code in the first line, rather than producing lots of redundent code. an explanation would help here as the lesson makes no sense to me


#20

found the solution. I was not assigning value to variable animals when inserting cobra..... facepalm.

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

Your code here!

duck_index = animals.insert(2, "cobra")

animals.insert(duck_index,"corba")

print animals # Observe what prints after the insert opera
Running above code gives that "cobra" is not at the correct location (index 4)

My original code also gives a warning "duck_index has value None.....":
animals = ["aardvark", "badger", "duck", "emu", "fennec fox"]
duck_index = animals.index("duck") # Use index() to find "duck"
duck_index = animals.insert(2, "cobra")

print animals # Observe what prints after the insert operation


#21

thank you so much :smiley: