Python programming integers are immutable

A Surprising Feature of Python Lists

What do you expect the output of the following Python code to be?

Depending on how well you understand how Python works under the hood, you may be surprised at this result. You might reason that since my_list is defined in the global scope, it has no business being changed when its value is passed into mutate_or_not() as a paramter. Let’s explore what is happening here.

You can step through the code with a great online visualization tool here.

Python List Mutation

Notice how the list is pointed to by both the my_list variable in the global frame AND the the a_list parameter inside mutate_or_not().

Compare the situation above to the following:

Look at the image below:

Code tracing available here. Can you see how there is no link between my_int (in the global frame), and an_int, the function parameter?

A Practical Example of Python List Mutation

Suppose you wish to rotate the items in a list. After some thinking, you might come up with a solution like this:

You might expect the output to be a successfully rotated list: [4, 1, 2, 5]. However, it doesn’t work!

But didn’t you just say that lists were mutable?

The issue here is that when we reassign lst inside the function, the original assignment is lost, and lst now just references the local parameter lst with the new values. The original lst defined outside of the function remains intact.

So what can we do about this? Well one solution is given below, but it is very clumsy and not practical. It does illustrate the issue though so it’s worth looking at:

The reason this works is that we do not redefine lst inside the function. Instead we mutate its elements.

However a much better solution is to use Python list slicing, as discussed for example here. In Python, my_list[:] refers to the whole list. Using this fact, we can rewrite our rotate_list() function and leverage the immutability of Python lists to achieve the desired result:

Mutable and Immutable Data Types in Python

Mutability in Python doesn’t just apply to lists and integers, but other data types as well. We just focused on lists in this article to keep things programmatic. Below is a table for easy reference.

mutable immutable
list
dictionary
set
user-defined classes
int
float
decimal
bool
string
tuple
range

Conclusion

This article has been about the mutability of lists in Python programming. Knowing how this works will help you to avoid some potentially confusing and time-consuming bugs in your code.

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join our mailing list

Join our mailing list to receive awesome articles about learning Python and Computer Science in a fun and accessible way, straight to your inbox.

 Take your Python skills to the next level!

with our free email course on object oriented programming with Python