Difference Between Mutable and Immutable in Python

By Mohit Uniyal|Updated : June 13th, 2022

Difference Between Mutable and Immutable in Python: In Python programming language, whenever the internal state of the is chargeable, that object is known as a mutable object. So whenever the object is gone through an internal state change, it is called mutable, and if not then it is called an immutable object. The main difference between mutable and immutable objects in Python is based on their acceptance of internal change.

The mutable and immutable states in Python are internal. If the changes in the internal state of the object occur with time if they are mutual else immutable. We will see what is mutable and immutable in detail along with it we will discuss the difference between mutable and immutable in various aspects.

Download Formulas for GATE Computer Science Engineering - Programming & Data Structures

Table of Content

Difference Between Mutable and Immutable in Python

Apart from being different based on the susceptibility to the changes, there exists other difference between mutable and immutable objects in Python which are explained in the table provided below:

Key Differences Between Mutable and Immutable in Python

MutableImmutable
The objects can be modified after the creation as well.Objects can not be modified after the creation of the objects.
Classes that are mutable are not considered final.Classes that are immutable are considered final.
Thread unsafe.Thread-safe.
Classes are not made final for the mutable objects.Classes are made final for the immutable objects.
Example: Lists, Dicts, Sets, User-Defined Classes, Dictionaries, etc.Example: int, float, bool, string, Unicode, tuple, Numbers, etc.

Formulas for GATE Computer Science Engineering - Digital Logic

What is Mutable in Python?

In Python programming language, whenever an object is susceptible to internal change or has the ability to change its values is known as a mutable object. Mutable objects in Python are generally changed after creation. 

For mutable objects in Python, their values can be changed but the identity of the object remains intact. The objects in Python which are mutable are provided below:

  • Lists
  • Dicts
  • Sets
  • User-Defined Classes 
  • Dictionaries

Formulas for GATE Computer Science Engineering - Computer Organization & Architecture

What is Immutable in Python?

Similarly, when the objects in the Python are not susceptible to the internal change are known as the immutable objects. They can not be modified once they are created. For example, int in Python is immutable hence they can not be modified once they are created.

Objects in Python which are immutable are shown below:

  • int,
  • float,
  • bool,
  • string,
  • Unicode,
  • tuple
  • Numbers
  • Frozen Sets
  • User-Defined Classes

Check out some important topics related to the difference between mutable and immutable objects in Python in the table provided below:

Difference Between actual and formal parametersDifference Between Python and C++
Difference Between Algorithm and PseudocodeDifference between Metals and Non-Metals
Difference Between Array and List in PythonDifference Between Hard Copy and Soft Copy

Comments

write a comment

FAQs on Difference Between Mutable and Immutable

  • The major difference between mutable and immutable objects in Python is that mutable objects can be modified or changed after their creation but the immutable objects can not be modified after their creation. This makes immutable objects final objects.

  • The list is mutable in Python since it is changeable. The values in the list can be modified after creating it. This is the difference between mutable and immutable. Values assigned to the list are not final and are changeable.

  • The string in Python is known as an immutable object since it is not susceptible to change. If the string would have been mutable, the change could have been done to it which is the basic difference between mutable and immutable objects in Python. The values assigned to the strings can not be altered after it is created. The values remain final.

  • The tuple in Python is immutable. The tuple is immutable since it carries the final value in it and does not allow the changes. It carries a sting and list and since string is immutable hence it is immutable.

  • The difference between mutable and immutable as per the thread is that mutable objects are not considered safe for the thread whereas immutable objects are thread-safe. The mutable objects are unsafe and immutable objects are thread safe.

Follow us for latest updates