Table of contents
1.
Introduction
2.
What are Mutable Objects?
2.1.
Example of Mutable Objects in Python
2.2.
List
2.3.
Python
2.4.
Dictionary
2.5.
Python
2.6.
Sets
2.7.
Python
3.
What are Immutable Objects?
3.1.
Example of Immutable Objects in Python
3.2.
Tuples
3.3.
Python
3.4.
Strings
3.5.
Python
4.
Difference between Mutable and Immutable in Python
5.
Example of Mutable Objects in Python
5.1.
Python
6.
Example of Immutable Objects in Python
6.1.
Python
7.
Frequently Asked Questions
7.1.
In Python, are set elements mutable?
7.2.
Tuples are immutable, and lists are mutable. What happens when the element of the tuple is of type list??
7.3.
Why is a list mutable in Python?
8.
Conclusion
Last Updated: Aug 20, 2025
Easy

Difference Between Mutable and Immutable in Python

Introduction

Python is an object-oriented language where everything is treated as an object. Objects are categorized as mutable and immutable in Python. Mutable objects can be modified after creation, while immutable objects cannot. Each object has a unique ID and a fixed type (e.g., int, float, string, list). The key difference in mutable vs immutable objects in Python lies in whether their values can change after initialization.

Difference Between Mutable and Immutable in Python

 

What are Mutable Objects?

The objects that can be changed after creation are known as mutable objects. Listsarrayssetdictionarybyte array, etc., are some of the mutable data types in Python.

Usually, mutable objects are used whenever there is a need to change the size or content of the object during runtime.

Example of Mutable Objects in Python

List

  • Python

Python

myList = ["One", 1, False, 'c']
myList.pop()
print(myList)
myList.append(True)
print(myList)
myList[0] = 2
print(myList)
You can also try this code with Online Python Compiler
Run Code

Output:

['One', 1, False]
['One', 1, False, True]
[2, 1, False, True]

In the list, we can add and remove elements by using append() and pop() methods, respectively. We can also modify its content. So the list is a mutable data type. 

 

Dictionary

  • Python

Python

myDict = { "India" : "Delhi", 
"isAisan" : True
}
myDict.update({"States" : 29})
print(myDict)
myDict["India"] = "New Delhi"
print(myDict)
You can also try this code with Online Python Compiler
Run Code

Output:

{'India': 'Delhi', 'isAisan': True, 'States': 29}
{'India': 'New Delhi', 'isAisan': True, 'States': 29}

As seen in the example, we can add an element in the dictionary by using the add() function, and we can also change its content after creation. Therefore, the Python dictionary is mutable.

Sets

Like the dictionary, the set is also mutable.

  • Python

Python

mySet = set(('India', 'U.S.A', False, 1, False))
mySet.add(False)
print(mySet)
You can also try this code with Online Python Compiler
Run Code

Output

{False, 1, 'U.S.A', 'India'}

 

You can practice by yourself with the help of online python compiler.

What are Immutable Objects?

Immutable objects can't be changed after creation. Stringsbytestuples, and frozen sets are examples of immutable data types.

Immutable objects are faster than mutable objects.

Example of Immutable Objects in Python

Tuples

  • Python

Python

mytuple = ('a', 1, 2, 'b')
print(mytuple[0])
mytuple[0] = 'b'
You can also try this code with Online Python Compiler
Run Code

Output

mytuple[0] = 'b'
TypeError: 'tuple' object does not support item assignment

As seen in the example, we can access the elements of tuples using the indices, but we can't modify them, so it is immutable.

Strings

  • Python

Python

myString = "myName"
print(myString[2])
myString[2] = 'n'
You can also try this code with Online Python Compiler
Run Code

Output

myString[2] = 'n'
TypeError: 'str' object does not support item assignment

 

Similar to the tuple, we can't modify the strings. So, strings are also immutable.

Difference between Mutable and Immutable in Python

ParametersMutableImmutable
ModificationCan be changed after creationCannot be changed after creation
Memory usageGenerally more efficientMay use more memory due to creating new objects
ID/Memory addressRemains the same when modifiedChanges when a new value is assigned
Examples (Python)Lists, dictionaries, setsStrings, tuples, frozensets, numbers
Thread safetyLess safe in multi-threaded environmentsInherently thread-safe
Use casesWhen content needs frequent updatesWhen data integrity is crucial
PerformanceFaster for in-place modificationsSlower for modifications, faster for access

Also see,  Convert String to List Python

Example of Mutable Objects in Python

Here's an example demonstrating mutable objects in Python:

  • Python

Python

# List - a mutable object
fruits = ['apple', 'banana', 'cherry']
print("Original list:", fruits)

# Modifying the list
fruits[1] = 'blueberry'
fruits.append('date')
print("Modified list:", fruits)

# Checking the id (memory address)
print("List id before modification:", id(fruits))
fruits.pop()
print("List id after modification:", id(fruits))
You can also try this code with Online Python Compiler
Run Code

Output:

Original list: ['apple', 'banana', 'cherry']
Modified list: ['apple', 'blueberry', 'cherry', 'date']
List id before modification: 140324425579456
List id after modification: 140324425579456

Explanation: In this example, we use a list, which is a mutable object in Python. We can modify the list by changing elements, adding new ones, or removing them. Notice that despite these modifications, the id() of the list remains the same. This demonstrates that mutable objects can be changed in-place without creating a new object.

Example of Immutable Objects in Python

Now, let's look at an example with immutable objects:

  • Python

Python

# String - an immutable object
text = "Hello"
print("Original string:", text)

# Attempting to modify the string
new_text = text + " World"
print("New string:", new_text)

# Checking the id (memory address)
print("Original string id:", id(text))
print("New string id:", id(new_text))

# Reassigning the variable
text = "Hello World"
print("Reassigned string:", text)
print("Reassigned string id:", id(text))
You can also try this code with Online Python Compiler
Run Code

Output:

Original string: Hello
New string: Hello World
Original string id: 140324426311024
New string id: 140324426310768
Reassigned string: Hello World
Reassigned string id: 140324426310768

Explanation: In this example, we use a string, which is an immutable object in Python. When we try to modify the string by concatenating " World", a new string object is created. This is evident from the different id() values of the original and new strings.

When we reassign the variable text, it points to a new string object with a different id(). This demonstrates that immutable objects cannot be modified in-place. Instead, operations that seem to modify them actually create new objects, and variables are reassigned to these new objects.

Frequently Asked Questions

In Python, are set elements mutable?

Set is an unordered collection. We can't access the elements of the set by index. Set itself is mutable, but we can't change the elements it contains.

Tuples are immutable, and lists are mutable. What happens when the element of the tuple is of type list??

If a tuple contains a list, we can change the list's contents. This is an exception in immutability.

Example:

myTuple = (['a', 'b'], 1, 2)
print(myTuple)
myTuple[0].append('c')
print(myTuple)
You can also try this code with Online Python Compiler
Run Code

Output:

(['a', 'b'], 1, 2)
(['a', 'b', 'c'], 1, 2)

Why is a list mutable in Python?

In Python, a list is mutable because its elements can be changed after creation. You can modify, add, or remove items from a list without creating a new list, offering flexibility and efficiency for dynamic data handling.

Conclusion

In this article, we discussed mutable and immutable objects in Python and saw the difference between them. Python is a high-level language that witnessed incredible growth and popularity. Nowadays, it is used by many tech giants and is also used in AI/ML. 

Recommended Readings:

Live masterclass