Get a skill gap analysis, personalised roadmap, and AI-powered resume optimisation.
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.
What are Mutable Objects?
The objects that can be changed after creation are known as mutable objects. Lists, arrays, set, dictionary, byte 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.
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.
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.
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
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.
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.