Difference between Mutable and Immutable in Python | Data Types
In generic terminology,
Mutable object means, object that can be changed after creating it
Immutable object means, 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 Mutable and immutable Data type in Python?
All the variables having following data types are mutable.
- user-defined classes (unless specifically made immutable)
Now examples of Immutable Data type in Python.
All the variables having following data types are immutable.
Now take some questions. Getting answer for these will clear your thoughts on difference between mutable and immutable in Python.
Why is string mutable 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 string is getting updated.
But internally, it does not update the string; rather it creates the new string. This is all because string is immutable. It creates 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 concatination.
So here is 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 second question.
Why tuple is 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 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 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 list is mutable, why is tuple immutable data type? I was really confused that time.
Hope this post clears your doubt. If you have any question, write in 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 tuple is immutable variable in Python.
If you are looking to know more insight about Python, do read Python archive. I keep posting my Python tricks there to help you guys.