Mutating Lists
Lists are mutable.
In programming, 'mutate' means ‘change’. Mutable means we can change the lists.
We can:
- assign a new value to a list index
- add and remove items from a list
- change the order of items
Assigning new values
We can change any element of a list by assigning at an index.
For example, to change the second item in the list above to the "Nigeria"
, we can assign to index 1
, like this:
# The old list
countries = ["Kenya", "Ghana", "Ethiopia", "Zimbabwe"]
# Change the second item
countries[1] = 'Nigeria'
# The new list
print(countries) # ['Kenya', 'Nigeria', 'Ethiopia', 'Zimbabwe']
Adding an item to the end of a list
To add a new element to the end of the list, you use .append()
countries.append("Cameroon")
print(countries) # ['Kenya', 'Nigeria', 'Ethiopia', 'Zimbabwe', 'Cameroon']
Removing an item
To remove an item from a list, you use pop()
. With no arguments, pop removes the last element.
countries = ['Kenya', 'Nigeria', 'Ethiopia', 'Zimbabwe', 'Cameroon']
print(countries) # ['Kenya', 'Nigeria', 'Ethiopia', 'Zimbabwe', 'Cameroon']
countries.pop() # removes the last element, "Cameroon"
print(countries) # ['Kenya','Nigeria', 'Ethiopia', 'Zimbabwe']
Or, you can specify an index to remove, and pop()
will remove the element at that index.
print(countries) # ['Kenya','Nigeria', 'Ethiopia', 'Zimbabwe']
countries.pop(1) # Removes the element at index 1, "Nigeria"
print(countries) # ['Kenya', 'Ethiopia', 'Zimbabwe']
Check your understanding
Try this quiz to check how well you understand what list operations will do.
Practice
Solution Code (try for 5 minutes before peeking)
my_list = [10, 20, 30, 40, 50]
print(my_list)
# Assign the first list item the value 5
my_list[0] = 5
print(my_list)
# Assign the last list item the value 'dog'
my_list[4] = 'dog'
print(my_list)
# Remove the second item in the list
my_list.pop(1)
print(my_list)
# Add another item to the end of the list with value False
my_list.append(False)
print(my_list)
print("The number of items in the list is", len(my_list))