2) More Data Types Lesson

Is a Python List Mutable

9 min to complete · By Martin Breuss

When you're able to change an object after you've created it, you say that it is mutable. Lists in Python are mutable, which gives you a whole set of new possibilities but also some challenges over immutable data types, such as tuples.

Changing Elements In A List

First, you'll look at some of the cool things that you can do due to the fact that lists are mutable. Remember your bucket list:

bucket_list = ["climb Mt. Everest", "eat fruits from a tree"]

After spending a month in solitude and pondering about life, the universe, and everything, you realize that the second entry doesn't quite cover what you tried to express with it. You want to keep your bucket_list object intact, but now you want to reach in there, grab the second item, and change it to match what you wanted to say. Because lists are mutable, you can do just that:

bucket_list[1] += " that I planted"
print(bucket_list)  
# OUTPUT: ["climb Mt. Everest", "eat fruits from a tree that I planted"]

In this code snippet, you're using indexing to access the second element in your bucket_list. This is the same process that you've seen in the previous lesson. However, now you're not just accessing the element, but you are changing it. Using the += operator, you're creating a new string object that consists of the content of your old string ("eat fruits from a tree") and a new string that you concatenated to the end of it (" that I planted").

You can see both types of mutability in action here:

  1. Immutable: Strings are immutable, so you need to create a new string object by concatenating the two strings. The new string object points to the value "eat fruits from a tree that I planted". However, you are not creating a new list object that has the second value replaced.
  2. Mutable: Instead, you're replacing the second element inside the existing bucket_list object. You do this by accessing it (bucket_list[1]) and replacing its value (+= " that I planted").
Colorful illustration of a light bulb

Note: Remember that += is a shorthand assignment operator. a += 1 is the same as a = a + 1. Return to the operators section in the previous module to get a refresher on Python operators and how to use them.

By changing an element in your list, you've effectively changed the existing list object. If you attempted to do the same with a tuple, you'd run into a descriptive TypeError.

Go ahead and give it a try! You might also find out that what you're doing here is called item assignment. It means that you're assigning a value to an item in your list.

Working with mutable objects can be useful, but it also has some drawbacks. Sometimes, strange things can happen when you forget that a list has changed due to some action you took in another part of your code.

Aliasing

The mutability of lists can be especially tricky when you have created an alias of a list.

Variables are pointers to values. You can have more than one pointer that points to the same value. This is called aliasing, and in the case of mutable objects, such as lists, it can lead to undesired results:

a = [1, 2, 3]
b = [1, 2, 3]

print(a == b)  # True
print(a is b)  # False

In this code snippet, you've created two lists, a and b, that both have the same values. However, they are pointing to different objects in memory. You're testing this with the equality operatory (==) and the identity operator (is).

Colorful illustration of a light bulb

Info: If you want a refresher on the different Python operators and how they work, check back to the relevant section in the first module of this course.

So you've established that you have two different lists that both hold the same values. Now you're reassigning one of your variables to the first list so that both variables a and b now point to the same list object:

b = a
print(a == b)  # True
print(a is b)  # True

There's nothing wrong with doing this, but it can introduce subtle bugs if you don't stay aware of what you're doing here. You might think that you're still referencing different objects with a and b, but in reality, they point to one and the same list. Therefore, and because you can change lists if you now make a change to b, then it'll also be reflected in a:

b[0] = 4
print(a)  # OUTPUT: [4, 2, 3]

After reassigning b to a, you were only dealing with one list object anymore. However, you had two references to the same object. This is called aliasing. Now, when you use the variable b to change something in your list object, you'll also see the same changes when you access the list through your other reference to it, a.

Mutability and Aliasing

Check out this great interactive visual explanation to drill your understanding of how mutability can have confusing effects when you alias lists.

Summary: Is a Python List Mutable

  • Lists are mutable, which means that you can change a list object in place. This can be helpful if you want your list to change state throughout your program. However, it's also a common source of confusion and bugs, especially when you're using aliasing to refer to the same list object with different variables.
  • Lists are a commonly used data type in Python programs, so you'll get to know some useful list methods in the next lesson.