Manipulating lists for fun and profit
You may be familiar with adding individual data elements to a list by using the .append()
method. However, if you want to combine a list with another array type (list, set, tuple), you
can use the .extend()
method on the list.
You can also use the .index()
method to find the position of an item in a list. You can then use that position to remove the item with the .pop()
method.
In this exercise, you'll practice using all these methods!
This exercise is part of the course
Data Types in Python
Exercise instructions
- Create a list called
baby_names
with the names'Ximena'
,'Aliza'
,'Ayden'
, and'Calvin'
. - Use the
.extend()
method onbaby_names
to add'Rowen'
and'Sandeep'
and print the list. - Use the
.index()
method to find the position of'Rowen'
in the list. Save the result asposition
. - Use the
.pop()
method withposition
to remove'Rowen'
from the list.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a list containing the names: baby_names
baby_names = ____
# Extend baby_names with 'Rowen' and 'Sandeep'
____
# Find the position of 'Rowen': position
position = ____
# Remove 'Rowen' from baby_names
____
# Print baby_names
print(baby_names)