Difference Between Mutable and Immutable in Python With Examples
By Rohit Sharma
Updated on Oct 07, 2025 | 12 min read | 2.57K+ views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Oct 07, 2025 | 12 min read | 2.57K+ views
Share:
Table of Contents
Mutable and immutable in Python refer to whether an object’s value can change after it’s created. Mutable objects, like lists and dictionaries, can be updated or modified in place. Immutable objects, such as strings and tuples, cannot be altered once assigned; any change creates a new object in memory. Understanding this difference is key to writing efficient and predictable Python code.
In this guide, you’ll read more about what makes data types mutable or immutable, key differences in behavior, practical examples, effects on memory and functions, common use cases, and best practices for using both types effectively.
Shape your future with upGrad’s Data Science Course. Gain hands-on expertise in AI, Machine Learning, and Data Analytics to become a next-generation tech leader. Enroll today and accelerate your career growth.
In Python, every value you create is stored as an object. The main difference between mutable and immutable in Python lies in how these objects behave when you try to change their values.
A mutable object allows you to modify its contents after creation. Lists, sets, and dictionaries fall into this category.
An immutable object, on the other hand, cannot be changed once created. Strings, tuples, and integers are common examples. Any “change” to them actually creates a new object in memory.
Advance your career in Data Science and AI, enroll in our expert-led programs to gain cutting-edge skills, drive innovation, and stay ahead in the world of intelligent technology.
Mutable and immutable objects handle memory differently:
| Aspect | Mutable Objects | Immutable Objects | 
| Definition | Can be changed after creation | Cannot be changed once created | 
| Examples | List, Dictionary, Set | String, Tuple, Integer | 
| Memory Allocation | Same memory location even after modification | New memory location created after modification | 
| Object ID | Remains the same | Changes after re-assignment | 
| Hashable | Usually not hashable | Hashable and can be used as keys in dictionaries | 
Popular Data Science Programs
Let’s see what happens when you modify both types of objects.
Mutable Example (List):
my_list = [1, 2, 3] 
print(id(my_list))      # 140356251483520 
my_list.append(4) 
print(id(my_list))      # 140356251483520  → same ID, object modified 
  Immutable Example (String):
my_string = "Hello" 
print(id(my_string))     # 140356252080368 
my_string += " World" 
print(id(my_string))     # 140356252081920  → new ID, new object created 
  Observation:
The list keeps the same memory address when changed, while the string creates a new object.
Example:
def modify_list(a): 
    a.append(10) 
 
def modify_string(s): 
    s += "!" 
     
lst = [1, 2, 3] 
text = "Hi" 
 
modify_list(lst) 
modify_string(text) 
 
print(lst)   # [1, 2, 3, 10] 
print(text)  # "Hi" 
  Here, the list is modified inside the function, but the string remains the same.
Also Read: Top 7 Data Types in Python: Examples, Differences, and Best Practices (2025)
Mutable vs Immutable Data Types in Python
  Change in Value → Same Object ID (Mutable) 
   Change in Value → New Object ID (Immutable) 
  You can visualize this as:
Mutable: ➡ [Original Object] → Modified in place
Immutable: ➡ [Original Object] → [New Object Created]
Understanding the difference between mutable and immutable in Python helps you manage memory better, avoid side effects, and write cleaner, more predictable code.
Also Read: Mutable vs. Immutable Objects in Python: Learn The Real Difference
Data Science Courses to upskill
Explore Data Science Courses for Career Progression
Mutability in Python means that an object’s content can be changed after it’s created. You can add, remove, or modify elements without creating a new object in memory. This is what makes mutable objects flexible for tasks that need constant updates.
When you work with a mutable data type, Python keeps the same memory address (object ID) even after you modify its content. So, instead of creating a new object, it directly updates the existing one.
Common mutable data types include:
Each of these can be changed after creation. Let’s look at a simple example.
my_list = [1, 2, 3] 
print(id(my_list))     # e.g. 140122902013264 
 
my_list.append(4) 
print(id(my_list))     # same ID → modified in place 
  The memory address stays the same, showing that Python updated the existing list instead of creating a new one.
Also Read: List, Tuple, Set, Dictionary in Python: Key Differences with Examples
When you modify a mutable object, Python changes the value inside the same memory space.
Here’s a simple comparison to understand it:
| Operation | Mutable Object (List) | Memory Behavior | 
| Add/Remove Elements | Allowed | Changes occur in the same memory block | 
| Object ID Before Change | Same | Unchanged | 
| Object ID After Change | Same | Unchanged | 
| Example | [1, 2, 3] → [1, 2, 3, 4] | Same memory address used | 
This property makes mutable types ideal for use cases like growing lists or updating dictionaries where performance matters.
person = {"name": "Alice", "age": 25} 
print(id(person))     # 140356250301840 
 
person["age"] = 26 
print(id(person))     # same ID → updated in place 
  The dictionary keeps the same identity. Only the value of a key changes.
Mutability affects how Python handles variables and function calls:
Shared References:
If two variables point to the same mutable object, changing one affects the other.
a = [1, 2, 3] 
b = a 
b.append(4) 
print(a)  # [1, 2, 3, 4] 
  In Functions:
Passing a mutable object to a function allows the function to modify it directly.
def add_item(x): 
    x.append(5) 
nums = [1, 2, 3] 
add_item(nums) 
print(nums)  # [1, 2, 3, 5] 
  This happens because both references point to the same memory location.
Also Read: 4 Built-in Data Structures in Python: Dictionaries, Lists, Sets, Tuples
You should use mutable data types when:
Imagine a container holding items.
When you change a mutable object, you simply replace or add items in the same container, rather than creating a new one.
Mutable:  [A, B, C]  →  [A, B, C, D]  (same container) 
Understanding what mutability means in Python helps you write code that’s more predictable and memory-efficient. You’ll know when your changes affect the original data and when Python creates something entirely new. 
Also Read: Top 50 Python Project Ideas with Source Code in 2025
Immutability in Python means that once an object is created, its value cannot be changed. Any attempt to modify it results in the creation of a new object in memory. The original object remains untouched.
This concept is central to understanding the difference between mutable and immutable in Python. Immutable objects provide stability, consistency, and safety, especially when working with data that should not change during execution.
Some of the most used immutable types are:
Each of these objects cannot be changed once created. If you try to modify them, Python creates a new object with a new memory address.
Also Read: Variables and Data Types in Python [An Ultimate Guide for Developers]
When you reassign or modify an immutable object, Python allocates new memory for the updated value while keeping the old object intact.
Here’s how that looks:
| Aspect | Immutable Objects | Example | 
| Definition | Cannot be changed after creation | Strings, Tuples | 
| Memory Allocation | New memory block created after change | "Hi" → "Hi World" | 
| Object ID | Changes after re-assignment | Different before and after modification | 
| Hashable | Yes | Can be used as dictionary keys | 
message = "Hello" 
print(id(message))     # e.g. 140122902013264 
 
message += " World" 
print(id(message))     # new ID → new object created 
  Even though it looks like the string is being updated, Python is actually creating a new string object and assigning it to the variable message. The original “Hello” object still exists separately in memory.
numbers = (1, 2, 3) 
print(id(numbers)) 
 
numbers += (4,) 
print(id(numbers))     # new object created 
  Tuples are also immutable. Adding an element doesn’t change the existing tuple—it builds a completely new one.
Also Read: The Efficient Memory Management in Python Guide
Immutability offers several advantages:
When you pass an immutable object to a function, the function cannot modify it.
It can only create and return a new object.
def change_string(s): 
    s += "!" 
    return s 
 
text = "Python" 
new_text = change_string(text) 
 
print(text)      # "Python" 
print(new_text)  # "Python!" 
  Here, text remains unchanged. The function creates and returns a new string instead.
Think of an immutable object as a sealed box.
If you want to “change” it, you must create a new box instead of altering the existing one.
Immutable:  [A, B, C]  →  [A, B, C, D]  (new box created) 
  Understanding what immutability means in Python helps you write cleaner, more reliable code. By recognizing when to use immutable types, you can avoid accidental data changes and make your programs easier to debug and maintain.
Even though mutable and immutable objects behave differently, they share some important characteristics in Python. Understanding these similarities helps you work with all data types more effectively.
a = [1, 2, 3]   # Mutable 
b = "Hello"     # Immutable 
 
print(type(a))  # <class 'list'> 
print(type(b))  # <class 'str'> 
  x = [1, 2] 
y = x      # Both point to the same mutable object 
x = [3, 4] # x now points to a new object 
  Even with immutable types:
p = "Python" 
q = p 
p = "Java" # p points to a new string object 
  | Operation | Mutable Example | Immutable Example | 
| Add/Append | lst.append(4) → modifies list | s += "!" → creates new string | 
| Multiply | lst * 2 → creates new list | s * 2 → new string | 
| Slice | lst[1:] = [5,6] → modifies | s[1:] → new string | 
Also Read: Python Keywords and Identifiers
lst = [1, 2] 
print(id(lst))   # Mutable 
 
text = "Hi" 
print(id(text))  # Immutable 
  These shared traits show that while the behavior differs, the core principles of Python objects apply to both, making it easier to reason about code across types.
The difference between mutable and immutable in Python defines how data objects behave when modified. Mutable objects like lists, dictionaries, and sets can be changed in place, while immutable objects like strings, tuples, and integers create new objects when altered. Understanding the difference between mutable and immutable data types in Python helps you manage memory effectively, avoid unexpected changes, and write more predictable, reliable code. Using the right type in the right context ensures your programs are efficient and easier to maintain.
Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!
Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!
Mutable objects in Python can be changed after creation, while immutable objects cannot. Lists, sets, and dictionaries are mutable, whereas strings, tuples, and integers are immutable. Understanding this difference helps manage memory, predict object behavior, and write safer, more efficient Python code.
Mutability depends on whether Python allows an object’s content to change. Mutable objects provide flexibility for updates and modifications, whereas immutable objects ensure stability and safety. This distinction helps Python manage memory efficiently and allows immutable objects to be used as keys in dictionaries.
Common mutable objects include lists, dictionaries, sets, and bytearrays. These objects can be updated, elements can be added or removed, and the same memory location is maintained. Mutable objects are ideal when frequent updates or changes are required in a program.
Immutable objects include strings, tuples, integers, floats, booleans, and frozensets. Once created, their values cannot be changed. Any modification results in a new object being created, leaving the original object intact, ensuring predictable behavior and memory safety.
Mutable objects retain the same memory address when updated, which allows efficient in-place changes. Immutable objects require new memory allocation for every change. Understanding this helps manage performance and avoid unexpected results when multiple variables reference the same object.
No, immutable objects cannot be directly modified. Any operation that appears to change them actually creates a new object in memory. For example, updating a string by concatenation generates a new string without affecting the original.
When mutable objects are passed to a function, changes inside the function affect the original object. Immutable objects, however, remain unchanged, and any modification inside the function creates a new object. This behavior is crucial for avoiding side effects in Python programs.
Lists are mutable. You can add, remove, or modify elements in a list without creating a new list object. This property makes lists suitable for dynamic data storage and iterative operations where frequent updates are needed.
Tuples are immutable. You cannot change, add, or remove elements once a tuple is created. Any operation that seems to modify a tuple actually produces a new tuple object. Tuples are useful when a fixed collection of elements is needed.
Strings are immutable to ensure data consistency and memory efficiency. Operations like concatenation or replacement create new string objects, preserving the original string. This makes strings safe to use as dictionary keys and ensures predictable behavior in Python programs.
Modifying a mutable object updates the content in the same memory location. Other variables referencing this object reflect the changes. This in-place update is memory-efficient but can lead to unintended side effects if not carefully managed.
Modifying an immutable object does not change the original object. Python creates a new object with the updated value. Variables referencing the original object remain unchanged, ensuring data safety and predictable code behavior.
Yes, immutable objects like strings, tuples, and integers are hashable and can be used as dictionary keys. Mutable objects, however, are not hashable and cannot be used as keys, because their content can change, affecting the hash value.
No, mutable objects like lists or dictionaries cannot be used as keys because their values can change, making them unhashable. Using mutable objects as keys would break the integrity of the dictionary’s key-value mapping.
Assignment in Python copies the reference to an object. For mutable objects, changes via one reference affect all references. For immutable objects, reassignment creates a new object, leaving other references unchanged. Understanding this is crucial for avoiding unexpected behavior in programs.
Knowing the difference between mutable and immutable data types in Python helps prevent bugs, manage memory, and write predictable code. It guides you in choosing the right object type for functions, data structures, and scenarios requiring safety or flexibility.
Mutable objects are ideal for dynamic data storage, iterative computations, and collections that change frequently, such as lists for appending items or dictionaries for updating key-value pairs. They enable in-place updates without creating new objects.
Immutable objects are best for fixed data, constants, dictionary keys, and thread-safe operations. They ensure consistency, prevent accidental changes, and can be safely shared across functions without side effects.
Python uses immutability to protect data from accidental modification. Immutable objects cannot be altered in place, so the original object remains unchanged. This design reduces bugs, improves predictability, and supports reliable programming practices.
Use mutable types when you need frequent updates or in-place modifications. Choose immutable types when you require stability, data safety, or hashable objects. Understanding the difference between mutable and immutable in Python helps you select the right type for efficiency and reliability.
840 articles published
Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...
Speak with Data Science Expert
By submitting, I accept the T&C and 
Privacy Policy
Start Your Career in Data Science Today
Top Resources