Mutable vs Immutable Objects in Python

Nikita Prasad Last Updated : 13 Sep, 2024
4 min read

Introduction

Python is an object-oriented programming language (or OOPs). In my previous article, we explored its versatile nature. Due to this, Python offers a wide variety of data types, which can be broadly classified into mutable and immutable types. However, as a curious Python developer, I hope you also wonder how these concepts impact data. How is data processed and manipulated in memory? How has it affected the quality of the program?  This article will provide a comprehensive overview of mutable vs immutable objects in Python and why they are crucial for effective programming. We will explore how mutability and immutability work across different Python objects, such as primitive data types like integers, floats, strings, etc., and built-in datatypes like lists, dictionaries, sets, tuples, etc.

What is Mutability vs Immutability?

From a high-level perspective, mutability refers to the ability of any object to be modified, changed, or updated after it is created. This means that if an object is mutable, you can change its state or content without creating a new object.

On the other hand, immutability means that once an object is created, its state cannot be changed/modified/updated. Any change to these objects creates a new object with a different memory allocation rather than altering the existing one.

What are Mutable vs Immutable Objects in Python?

The below image shows that Python’s rich data types can be divided into two categories: Mutable and Immutable objects, which are then further divided.

Mutable vs Immutable Objects in Python

Comparative Analysis of Python Data Types

Let’s have a look over a comparison between all the built-in datatypes:

Data TypeMutable/ImmutableDescriptionUse Case
IntegersImmutableWhole numbers (e.g., 1, -5, 42).Use when working with numerical data that does not change.
FloatsImmutableNumbers with decimal points (e.g., 3.14, -0.001).Useful for scientific computations, financial data, etc.
BooleansImmutableLogical values: True or False.Conditional checks, logical operations.
StringsImmutableSequence of characters (e.g., “hello”, “world”).Used for text manipulation, document processing, etc.
TuplesImmutableOrdered collection of items (e.g., (1, 2, 3)).Suitable for constant data, it can be used as dictionary keys.
Frozen SetsImmutableAn unordered collection of unique items, an immutable version of a set.Used in cases where the set needs to be constant and hashable.
Complex NumbersImmutableNumbers with real and imaginary parts (e.g., 1 + 2j).Used in scientific computing, signal processing, etc.
ListsMutableOrdered collection of items (e.g., [1, 2, 3]).Use when you need to modify, add, or remove elements frequently.
DictionariesMutableCollection of key-value pairs (e.g., {“name”: “John”, “age”: 30}).Ideal for mapping relationships, lookups, and data storage.
SetsMutableUnordered collection of unique items (e.g., {1, 2, 3}).Best used for membership testing, removing duplicates, etc.
Custom Objects (Classes)Mutable/ImmutableBehavior depends on how the class is defined (mutable by default).Tailored behavior based on requirements; can control mutability.

To understand these concepts in a more Pythonic way, go through these –

  1. Primitive Datatypes are “Immutable” – Link
  2. Python Built-in Data Structures are “Mutable” – Link

In these articles, I have discussed the mutability and immutability of these datatypes, the `id` function, shallow and deep copy, and more, along with codes.

Note: However, I recommend only checking those codes after reading this article. This article enhances your understanding of “What happens inside the memory space?”

What Happens at the Memory Level?

When discussing immutability at the memory level, an immutable object cannot be altered directly. Any operation that seems to modify an immutable object creates a new object with the modified value in memory. Mutable objects share the same memory allocated previously. Changes to these objects occur in place, modifying the existing memory content without new allocation.

Before exploring this further, let’s first understand the two most common concepts about deleting objects from memory.

  1. Deallocation means that the system frees and makes available for other uses the memory previously occupied by an object.
  2. Garbage collection is a process in Python that automatically finds and frees up memory that is no longer being used by the program, especially for objects that reference each other in a cycle.

How Does Deletion of Objects Work?

Python’s memory management relies on two major things, reference counting and garbage collectors, to handle the deletion of objects. Let’s understand them one by one:

  1. Reference Counting: Python tracks the number of references pointing to each object. This is called the reference count.
  2. Cyclic References — Garbage Collection: Python also has a garbage collector that handles cyclic references. Sometimes, objects reference each other in a loop. When the reference count drops to zero, the memory occupied by the object is deallocated. For example, object A references object B and object B references object A. Even if no other part of the program needs these objects, their reference counts never drop to zero because they reference each other. This is where the garbage collector steps in.

How is the Performance of a Program Determined?

In terms of performance implications, mutability and immutability have significant differences. Immutable data types are generally faster to access and process. Python can optimize memory usage by reusing immutable objects, mainly if you’re working with small integers and strings across the program.

Mutable data types are more flexible but can incur additional overhead due to the need for dynamic memory space resizing. For instance, lists in Python are dynamic arrays because they are stored in a way that allows them to grow and shrink in size while performing operations such as adding or deleting elements.

Conclusion

In conclusion, understanding the difference between mutable and immutable objects is crucial for writing efficient and reliable code in Python. For example, immutability offers safety where data should not change, such as in key-value mappings or concurrent programming.

Conversely, mutability is helpful in scenarios where dynamic updates to data structures are necessary for that particular part of the program. Knowing when to use what is essential to understanding the trade-offs in performance and complexity, ultimately leading to writing maintainable programs.

Also Read: Comprehensive Guide to Python Built-in Data Structures

Frequently Asked Questions

Q1. What is the difference between mutable vs immutable objects in Python?

A. Mutable objects, like lists or dictionaries, offer the flexibility of in-place modification after their creation. Meanwhile, immutable objects, such as tuples or strings, cannot be altered after creation in the same memory allocation.

Q2. Why are strings immutable in Python?

A. Strings are immutable to optimize memory usage and allow safe sharing across different program parts. This reduces memory usage for frequently used strings and simplifies reasoning about string handling in multi-threaded environments.

Q3. How does immutability affect performance in Python?

A. Immutable objects can lead to faster performance because they are easier to manage in memory. Python can reuse immutable objects, reducing the overhead of creating new objects repeatedly. This adds insight into memory management benefits.

Hi-ya!!! 👋

I'm Nikita Prasad

Data Analyst | Machine Learning and Data Science Practitioner

↪️ Checkout my Projects- GitHub: https://github.com/nikitaprasad21

Know thy Author:

👩🏻‍💻 As an analyst, I am eager to gain a deeper understanding of the data lifecycle with a range of tools and techniques, distilling down data for actionable takeaways using Data Analytics, ETL, Machine Learning, NLP, Sentence Transformers, Time-series Forecasting and Attention to Details to make recommendations across different business groups.

Happy Learning! 🚀🌟

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details