Aan de slagGa gratis aan de slag

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!

Deze oefening maakt deel uit van de cursus

Data Types in Python

Cursus bekijken

Oefeninstructies

  • Create a list called baby_names with the names 'Ximena', 'Aliza', 'Ayden', and 'Calvin'.
  • Use the .extend() method on baby_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 as position.
  • Use the .pop() method with position to remove 'Rowen' from the list.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# 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)
Code bewerken en uitvoeren