<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>


Replace this line with your code.

<do not remove the three backticks above>

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

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

What is the purpose of adding **duck_index?**
Even in the second part 
I know we are inserting cobra however, why is there a need for duck_index in the second function? 
print animals # Observe what prints after the insert operation


The .insert() instruction gives you the ability to add an element into a specific point within the list. Let’s say you want to add ‘cobra’ into the list of animals. But you want to add it to become the 3rd element.

You could do:

animals.insert(2, 'cobra')


But what if you wanted to add that new element before another known animal? How would you do that? Think about it.

You’d have to find where that known animal is within the list. You need it’s index within the list.

The variable duck_index then is storing the location of where you want to insert the new animal ‘cobra’. It’s a dynamic way of:

  1. Find where you want to insert it
  2. Store that index location
  3. And then insert the new element into the list at that index

Does that make sense?


The second functions I understand now that you can either place
animals.insert(2, “Cobra”

Can you please break down the first part for me please

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

I seem to not understand the purpose for this function


Let me try a different approach for you to connect the dots for you.

Let’s start with the purpose of the exercise. It wants you to dynamically insert another animal into the list before the animal “duck”. Think about that.

Let’s assume that you don’t know that “duck” is sitting in the 3rd element slot and has an index of 2. How could you accomplish the task of inserting “cobra” before “duck” within the list?

Imagine that this code was inside of a function and the function doesn’t know anything about the incoming list of animals.

Strategy then is:

  1. Find where “duck” is in the list and grab it’s index
  2. Then use that index to insert the new animal before “duck”

You accomplish step 1 above with this line of code:

duck_index = animals.index(“duck”)

It’s grabbing the index of “duck” within the list called animals. And then that integer value is assigned to the variable called duck_index. Now you have the insertion point. Right?


Now it is more clear thank you so much.


You are very welcome. Have fun with it.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.