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 following major two types in Python – Mutable and Immutable.
What is Mutable and Immutable Object:
In generic terminology,
Mutable object means an object that can be changed after creating it
Immutable object means an object that can not be changed once you create it.
Same applies for the Variable.
Difference between mutable and immutable in Python:
The variable, for which we can change the value is called mutable variable.
The variable, for which we can not change the value is 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 type in Python?
All the variables having the following data types are mutable.
- user-defined classes (unless specifically made immutable)
Now examples of Immutable Data type in Python.
All the variables having the following data types are immutable.
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.
list_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 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  = 6 # success # alter the tuple (change any value in the tuple) tup_Num  = 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 on it.
- What is the difference between mutable and immutable in Python?
- What are mutable and immutable variables?
I described all by giving example. Then he asked me…
- If the list is mutable, why is tuple immutable data type? I was really confused at that time.
Also, check the difference between tuple and list in Python.
Hope this post clears your doubt. If you have any question, write in the comment section. I will reply back to you.
- If you want to write most efficient code, you should be the knowing difference between mutable and immutable in python.
- Concatenating string in loops wastes lots of memory. Use list compression join technique.
- Though, list and tuple seems similar syntactically, 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.