Last modified: Feb 15, 2025 By Alexander Williams

Mutable vs Immutable Types in Python

In Python, understanding the difference between mutable and immutable types is crucial. It affects how data is stored, modified, and passed around in your code. Let's dive into what makes a type mutable or immutable and how to use them effectively.

What Are Mutable Types?

Mutable types are objects whose state can be changed after creation. This means you can modify their content without creating a new object. Common mutable types in Python include lists, dictionaries, and sets.

For example, consider a list:


# Example of a mutable list
my_list = [1, 2, 3]
my_list.append(4)  # Modifying the list
print(my_list)


# Output
[1, 2, 3, 4]

Here, the list my_list is modified by adding a new element. The original list object is changed, not replaced.

What Are Immutable Types?

Immutable types are objects whose state cannot be changed after creation. Any modification creates a new object. Common immutable types in Python include integers, strings, and tuples.

For example, consider a string:


# Example of an immutable string
my_string = "Hello"
new_string = my_string + " World"  # Creating a new string
print(new_string)


# Output
Hello World

Here, the original string my_string remains unchanged. A new string new_string is created instead.

Key Differences Between Mutable and Immutable Types

The main difference lies in how they handle modifications. Mutable types allow in-place changes, while immutable types require creating a new object for any change.

This difference impacts memory usage and performance. Mutable types can be more efficient for frequent modifications. Immutable types ensure data integrity and are safer in concurrent environments.

For more on variable types, check out Understanding Python Variable Types.

Examples of Mutable and Immutable Types

Let's look at more examples to solidify your understanding.

Mutable Example: Dictionary


# Example of a mutable dictionary
my_dict = {'name': 'Alice', 'age': 25}
my_dict['age'] = 26  # Modifying the dictionary
print(my_dict)


# Output
{'name': 'Alice', 'age': 26}

The dictionary my_dict is modified in place.

Immutable Example: Tuple


# Example of an immutable tuple
my_tuple = (1, 2, 3)
# my_tuple[0] = 4  # This will raise an error
new_tuple = my_tuple + (4,)  # Creating a new tuple
print(new_tuple)


# Output
(1, 2, 3, 4)

The original tuple my_tuple remains unchanged. A new tuple new_tuple is created.

Why Does It Matter?

Understanding mutability helps you write efficient and bug-free code. For instance, when passing arguments to functions, mutable objects can be modified within the function, affecting the original object.

For more on variable behavior in functions, see Managing Persistent Variables Across Function Calls.

Conclusion

In Python, mutable and immutable types serve different purposes. Mutable types like lists and dictionaries allow in-place modifications, while immutable types like strings and tuples ensure data integrity. Knowing when to use each can greatly improve your code's efficiency and reliability.

For further reading, explore Variable vs String in Python: Key Differences.