Difference between Mutable and Immutable in Python | Data Types

Difference between Mutable and Immutable in Python | Data Types

Every variable we create in our programming has data types associated with it. This shows the type of value we can save in that variable. These variables can be distinguished into the following major two types in Python – Mutable and Immutable.

What is a Mutable and Immutable Object?

In generic terminology,

A mutable object means an object that can be changed after creating it.

An immutable object means an object that can not be changed once you create it.

The same applies to the Variable.

Difference between mutable and immutable in Python

The variable, for which we can change the value is called a mutable variable.
The variable, for which we can not change the value is an immutable variable.

In the case of Python, both types of variables are available.

If you try to change the immutable object, instead of altering the object, it returns a new object.

What are the examples of the Mutable and Immutable Data types in Python?

All the variables having the following data types are mutable.

  • list
  • dict
  • set
  • bytearray
  • user-defined classes (unless specifically made immutable)

Now examples of Immutable Data types in Python.

All the variables having the following data types are immutable.

  • int
  • float
  • decimal
  • complex
  • bool
  • string
  • tuple
  • range
  • frozenset
  • bytes

You can easily check the datatype of any variable in Python.

Now take some questions.  Getting the answers for these will clear your thoughts on the difference between mutable and immutable in Python.

Why is String Immutable if it can be Concatenated?

Talking about the String mutability, many geeks are confused.

ist_Name =["Bob", "Alley", "Mike"];

str_Output = ""
for data in list_Name:
     string_Output += str(data)

Look at the above program. It is a simple program that concatenates the string. You might be saying; it should be mutable as the string is getting updated.

But internally, it does not update the string; rather it creates the new string. This is all because the string is immutable. It creates a third string by concatenating two strings.

And this is the reason, this code of concatenation is not efficient as it creates a new string every time you call for concatenation.

If you are iterating through the concatenation, lots of memory can be wasted for creating a temporary string and throwing it out after concatenation.

So here is a simple and most effective solution…

list_Name =["Bob", "Alley", "Mike"];

# Another way is to use a
#list comprehension technique in Python
"".join([str(data) for data in list_Name])

It concatenates all the list object values and then converts it into a string. As concatenation occurs at the list (mutable), no new object is created.

Now the second question.

Why is Tuple Immutable Data Type in Python?

If list and dictionary are mutable variables, it’s really confusing as a tuple is immutable. But it is.

Take a list (mutable object) and a tuple (immutable object). Compare what it can be after updating values.

Let’s take an example to prove it by comparing the tuple with the list.

# Create a list variable
list_Num = [5, 7, 9, 11]

# Create a tuple variable (initialize with the same values)
tup_Num = (5, 7, 9, 11)

# print the list
list_Num
[5, 7, 9, 11] # output

# print the tuple
tup_Num
(5, 7, 9, 11) # output

# alter the list (change any value in the list)
list_Num [1] = 6 # success

# alter the tuple (change any value in the tuple)
tup_Num [1] = 4 # error

# print the list again
list_Num
[5, 6, 9, 11] # There is a change in value; so called mutable.

# print the tuple
tup_Num
(5, 7, 9, 11) # None of the values are changed; so it is immutable.

I remember when I attended Druva telephonic interview, they asked me a couple of questions about it.

  • What is the difference between mutable and immutable in Python?
  • What are mutable and immutable variables?

I described it all by giving examples. Then he asked me…

  • If the list is mutable, why is the tuple immutable data type? I was really confused at that time.

Also, check the difference between the tuple and list in Python.

Hope this post clears your doubt. If you have any questions, write in the comment section. I will reply back to you.

Final Words:

  • If you want to write the most efficient code, you should be the knowing difference between mutable and immutable in python.
  • Concatenating string in loops wastes lots of memory. Use the list compression join technique.
  • Though list and tuple seem similar syntactically, the List is mutable and the tuple is an immutable variable in Python.

If you are looking to know more insight about Python, do read the complete Python tutorial. I keep posting my Python tricks there to help you guys.

Happy Programming!

12 Comments

  1. why list is mutable,tuple is immutable.plz explain with example and why values are changed in list,why values are not changed in tuple.

    1. Hi Lokesh,

      Many Python programmers find it difficult to understand. So I told its little confusing topic.

      Check out the code in this article under “Why tuple is immutable data type in Python?”

      You can see, in the list, we can change any value. In a case of the tuple, we can not change the value of the object (value) after creating the tuple.

      You can try running the program specified in this post.

    1. You can not add or delete elements from the tuple as it’s immutable.

      However, you can to it indirectly.

      Covert the tuple into a list.
      Add or delete elements from the list
      Convert list back to a tuple.

Leave a Reply

Your email address will not be published. Required fields are marked *