Learn
Sometimes you need to search for an item in a list.
animals = ["ant", "bat", "cat"] print animals.index("bat")
- First, we create a list called
animals
with three strings. - Then, we print the first index that contains the string
"bat"
, which will print1
.
We can also insert
items into a list.
animals.insert(1, "dog") print animals
- We insert
"dog"
at index 1, which moves everything down by 1. - We print out
["ant", "dog", "bat", "cat"]
Instructions
1.
Use the .index()
method to find the index of "duck"
. Assign that result to a variable called duck_index
.
Then use the .insert()
method with duck_index
as the first argument and the string "cobra"
as the second argument.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.