Difference Between Mutable and Immutable in Python With Examples

By Rohit Sharma

Updated on Oct 07, 2025 | 12 min read | 2.57K+ views

Share:

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. 

Key Difference Between Mutable and Immutable in Python 

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. 

1. Behavior in Memory 

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 

 

2. Example to Understand the Difference 

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. 

3. Functional Impact 

  • Mutable objects can lead to unexpected results when passed into functions because the function might modify the original object. 
  • Immutable objects stay safe because any change creates a new copy. 

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) 

4. When to Use Each 

  • Use mutable types when you need to update or expand data frequently (e.g., managing lists or caches). 
  • Use immutable types when you want consistency and data safety (e.g., dictionary keys, configuration values). 

5. Visual Summary 

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

background

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

Enroll yourself in a Job-ready Program in Artificial Intelligence & Machine Learning from upGrad and gain practical experience with Python Toolkit, SQL, and Large Language Models such as ChatGPT through this comprehensive Artificial Intelligence and Machine Learning Program.

What Is Mutability in Python? 

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. 

1. Examples of Mutable Objects in Python 

Common mutable data types include: 

  • List 
  • Dictionary 
  • Set 
  • Bytearray 

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 

2. How Mutability Works in Memory 

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. 

3. Example: Dictionary Mutation 

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. 

4. Practical Implications of Mutability 

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 

5. When to Use Mutable Types 

You should use mutable data types when: 

  • The content needs frequent updates. 
  • You’re building dynamic structures like lists, sets, or caches. 
  • You want to modify data in-place for better performance. 

6. Visualizing Mutability 

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 

What Is Immutability in Python? 

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. 

1. Common Immutable Data Types in Python 

Some of the most used immutable types are: 

  • String 
  • Tuple 
  • Integer 
  • Float 
  • Boolean 
  • Frozenset 

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] 

2. How Immutability Works in Memory 

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 

3. Example: String Immutability 

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. 

4. Example: Tuple Immutability 

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 

5. Why Immutability Matters 

Immutability offers several advantages: 

  • Data safety: Immutable data can’t be accidentally changed. 
  • Predictability: Values remain consistent across your code. 
  • Hashability: Immutable objects can be used as keys in dictionaries or elements in sets. 
  • Thread-safety: Ideal for concurrent or multi-threaded programs since values can’t be altered. 

6. Functional Impact of Immutability 

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. 

7. Visualizing Immutability 

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. 

Similarities Between Mutable and Immutable in Python 

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. 

1. Both Are Python Objects 

  • Every value in Python, whether mutable or immutable, is stored as an object
  • Each object has a type, value, and memory address (object ID)
  • Both types can be assigned to variables, passed to functions, and returned from functions. 
a = [1, 2, 3]   # Mutable 
b = "Hello"     # Immutable 
 
print(type(a))  # <class 'list'> 
print(type(b))  # <class 'str'> 
  

2. Both Follow Python’s Assignment Rules 

  • Variables in Python store references to objects, not the objects themselves. 
  • Reassigning a variable works the same for both mutable and immutable objects. 
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 
  

3. Both Can Be Used in Expressions 

  • You can perform operations like addition, multiplication, or concatenation on both types. 
  • For immutable objects, these operations create new objects. For mutable objects, some operations may modify the object in place. 

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 

4. Both Can Be Checked for Identity and Type 

  • You can check object ID and type for both mutable and immutable objects using id() and type(). 
  • This helps track changes or confirm whether a new object is created. 
lst = [1, 2] 
print(id(lst))   # Mutable 
 
text = "Hi" 
print(id(text))  # Immutable 
  

5. Both Are Integral to Python Programming 

  • Python’s flexibility comes from its ability to handle both mutable and immutable types efficiently. 
  • Both types have built-in methods and support operations specific to their type, enabling diverse programming patterns. 
      

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. 

Conclusion 

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

Promise we won't spam!

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!

Frequently Asked Questions (FAQs)

1. What is the difference between mutable and immutable in Python?

 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. 

2. Why are some Python objects mutable and others immutable?

 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. 

3. What are examples of mutable objects in Python?

 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. 

4. What are examples of immutable objects in Python?

 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. 

5. How does mutability affect memory allocation in Python?

 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. 

6. Can immutable objects be modified in Python?

 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. 

7. How does mutability impact function arguments?

 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. 

8. Are lists mutable or immutable in Python?

 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. 

9. Are tuples mutable or immutable in Python?

 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. 

10. Why are strings immutable in Python?

 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. 

11.What happens when you modify a mutable object?

 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. 

12. What happens when you modify an immutable object?

 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. 

13. Can immutable objects be used as dictionary keys?

 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. 

14. Can mutable objects be used as dictionary keys?

 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. 

15. How do mutable and immutable objects behave with assignment?

 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. 

16. Why is understanding mutability important in Python?

 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. 

17. What are practical use cases for mutable objects?

 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. 

18. What are practical use cases for immutable 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. 

19. How does Python ensure data safety with immutability?

 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. 

20. How can I choose between mutable and immutable types in Python?

 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. 

Rohit Sharma

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

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

upGrad Logo

Certification

3 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree

17 Months

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in DS & AI

360° Career Support

Executive PG Program

12 Months