Are Lists Immutable in Python?
In this article, we’ll explore the concept of list immutability in Python. We’ll define what immutable lists are, provide a step-by-step explanation, and examine code snippets to illustrate how lists …
Updated July 11, 2023
In this article, we’ll explore the concept of list immutability in Python. We’ll define what immutable lists are, provide a step-by-step explanation, and examine code snippets to illustrate how lists work in Python.
Definition of Immutable Lists
In programming, an immutable object is one whose state cannot be modified after it’s created. In other words, once you’ve created an immutable list, its contents cannot be changed. This is in contrast to mutable objects, which can have their state modified after creation.
Are Lists Mutable in Python?
Now, let’s address the question directly: are lists mutable in Python? The answer might surprise you. While it’s true that lists can appear to change when you add or remove elements, this isn’t quite the same as truly modifying a list object itself.
In Python, lists are implemented as dynamic arrays. When you create a new list using square brackets []
or the list()
function, Python allocates memory for a new array. Each time you append an element to the list or remove one, Python adjusts the internal array structure accordingly. This means that even though the list’s contents may change, its underlying object remains unchanged.
Step-by-Step Explanation
Let’s explore this using some examples:
Creating a New List
my_list = []
In this example, we create an empty list my_list
. Python allocates memory for a new array to store the list elements. The resulting object is immutable in the sense that its internal structure cannot be changed.
Appending an Element
my_list.append(42)
Here, we append the integer value 42 to our list using the append()
method. However, this doesn’t change the underlying object itself; it only adjusts the array structure within the existing object. The resulting object remains immutable.
Removing an Element
my_list.pop()
When we remove the last element from our list using the pop()
method, Python updates the internal array structure accordingly. Again, this doesn’t change the underlying object itself; it simply modifies its contents.
Code Snippets
Let’s look at some code snippets to see how these concepts play out:
Immutable List Creation
my_list = []
id_before_change = id(my_list)
# Now let's append an element...
my_list.append(42)
id_after_change = id(my_list)
print(f"Initial list ID: {id_before_change}")
print(f"List ID after appending 42: {id_after_change}")
When you run this code, you’ll see that the initial list object and the one created after appending an element are indeed the same. This illustrates how lists in Python remain immutable objects even when their contents change.
Using list()
for Immutability
original_list = [1, 2, 3]
immutable_list = list(original_list)
# Now let's try modifying the original list...
original_list.append(4)
print(f"Original List: {original_list}")
print(f"Immutable List: {immutable_list}")
In this example, we create an immutable copy of our list using the list()
function. As you can see from the output, attempting to modify the original list doesn’t affect the immutable copy.
Conclusion
In conclusion, while lists in Python appear mutable because their contents change over time, their underlying objects remain unchanged. This means that creating a new list object results in an immutable object whose state cannot be modified after creation.
Remember:
- Lists are implemented as dynamic arrays.
- When you create a new list using
[]
orlist()
, Python allocates memory for a new array. - Even though the list’s contents may change, its underlying object remains unchanged.
- Use the
id()
function to verify whether two lists share the same underlying object.
Hope this helps!