5 Examples of Mutable Datatypes in Python

In this article let us look at 5 examples of mutable datatypes in Python.

If you wish to develop your physique like Mr. Arnold, “understanding” how to perform exercises like squats, bench presses, etc., is not enough, you need to “consistently work your muscles” to build up your mass and strength!

Learning Python is no different,

  • 1st you need to understand the concepts
  • Then you need to practice those concepts

Hence this article will focus on strengthening our understanding of the concepts we have learned in our other article below.

Mutable and Immutable Data Types in python explain using examples

If you haven’t read that one yet I suggest reading that before continuing this one! There you will be able to find answers to questions such as

  • What are mutable datatypes?
  • How can we figure out if an object is mutable or immutable? and
  • Why do we need mutable objects?

Coming back to the topic, let’s start our Python workout today with the 1st example!

Example#1: Lists

As we might guess, list is a mutable type in Python. But have you ever wondered how exactly can we prove that?

There is a really clever way to check this, and that is by seeing the ID of the object before and after you change it.

If an object is mutable, the interpreter overwrites the object with the changes we requested.

On the other hand, if an object is immutable, the interpreter creates a new object which stores the new requested changes. Hence this will get assigned a new ID.

Enough theory, let’s see that in code!

# creating a list
my_list = [1,3,5,7]
# using the id() function to get the id
my_list_id = id(my_list)

# editing our list
my_list.append(9)
# saving the id in a different variable after the changes
my_new_list_id = id(my_list)

print("List ID before change:", my_list_id)
print("List ID after change:", my_new_list_id)
List ID before change: 140572508154048
List ID after change: 140572508154048

As we can see, the IDs are the same! This means that no new object was created, instead we were able to edit the existing object. This proves that lists are mutable!

Example#2: Dictionaries

Dictionaries are another common data type in Python, and they happen to be mutable as well!

Let’s check if this is true using the same ID method using id():

# creating a dictionary
my_dict = {1:"Sunday", 2:"Monday", 3:"Tuesday"}
# using the id() function to get the id
my_dict_id = id(my_dict)

# editing our dictionary
my_dict[4] = "Wednesday"
# saving the id in a different variable after the changes
my_new_dict_id = id(my_dict)

print("Dictionary ID before change and after is same:", my_dict_id == my_new_dict_id)
Dictionary ID before change and after is same: True

Looks like the ID didn’t change, it is safe to say dictionaries are mutable!

Example#3: Sets

A Set is another data type that is always mutable. Python allows us to modify their contents by adding or removing their elements.

#creating a set
my_set = {"apples", "oranges", "watermelons"}
#using the id() function to get the id
my_set_id = id(my_set)

#editing our set
my_set.add("strawberries")
#saving the id in a different variable after the changes
my_new_set_id = id(my_set)

print("Set ID before change and after is same:", my_set_id == my_new_set_id)
Set ID before change and after is same: True

We can clearly see that the IDs are same! 

Example#4: ByteArrays

ByteArrays are an interesting datatype used in image processing, text processing, and in all places where we need to exchange data in bytes.

If you are interested in learning more about bytes and their immutable counterparts bytes you can do so here

Python: bytearray vs bytes, Similarities & Differences Explained!

ByteArrays are a mutable sequence of Bytes. This is proven in the following code:

# creating a Byte array 
my_bytearray = bytearray([1,2,3,4,5,6,7,8,9])
# using the id() function to get the id
my_barray_id = id(my_bytearray)

# editing our Byte array 
my_bytearray.append(10)
# saving the id in a different variable after the changes
my_new_barray_id = id(my_bytearray)

print("Byte array ID before change and after is same:", my_barray_id == my_new_barray_id)
Set ID before change and after is same: True

As you can see from the output above, bytearrays indeed are mutable!

Here is a YouTube video we made on ByteArray if you wish to learn more!

Example#5: Arrays

Arrays are another common example of a datatype that is mutable. Here is the program showing their mutability:

And with that, I will end this article.

I hope you have gotten some practice on determining if a given datatype is immutable or mutable and you have learned something new!

Congratulations on making it to the end of the article, not many have the perseverance to do so!

I hope you enjoyed reading this article and found it useful!

Feel free to share it with your friends and colleagues!

If your thirst for knowledge has not been quenched yet, here are some related articles that might spark your interest!

Related Articles

Mutable and Immutable Data Types in python explain using examples

Python: bytearray vs bytes, Similarities & Differences Explained!

Python “is” versus “==”: Explained using Examples!

Thanks to Namazi Jamal for his contributions in writing this article!

Photo of author
Editor
Balaji Gunasekaran
Balaji Gunasekaran is a Senior Software Engineer with a Master of Science degree in Mechatronics and a bachelor’s degree in Electrical and Electronics Engineering. He loves to write about tech and has written more than 300 articles. He has also published the book “Cracking the Embedded Software Engineering Interview”. You can follow him on LinkedIn