Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Mutable Data Types in Python

Last Updated on March 13, 2023 by Prepbytes

Like in any other programming language python provides or supports various data types. Data types are used to store the data of the variable or expression according to its value. In python, the data types are classified into two categories: mutable and immutable. While moving further in this article we will discuss all mutable data types, their properties, examples, applications, etc.

What are Mutable Data Types in Python?

As the name suggests mutable data types in python are those data structures whose value can be changed after they have been created. In other words, if a variable is assigned with a mutable data type, we can modify its value during program execution. The mutable data types in python include lists, dictionaries, and sets.

Mutable data types in python differ from immutable data types in python as the value of immutable data types cannot be changed once assigned. We have to create a new object with the updated value.

Properties of Mutable Data Types

There are several properties of mutable objects that distinguish them from immutable objects some of them are mentioned below:

  • Changeable values: The main difference between the two is that we can change the value of a mutable data type after initialization during the program execution. So we can use this property when the data needs to be updated regularly.
  • Modifiable in place: In place refers to the same object as we can modify the same object we do not need to create a new object every time we need to change the data. This will save a lot of time and memory.
  • Flexible structure: Due to their flexible structure the developers can create or solve complex problems with them.
  • Can be used as keys in dictionaries: With the help of mutable data types, we can use them as keys and dictionaries which in return help the developers to access the data in a more convenient way.

Lists in Python

They are one of the most commonly used mutable data types in python. Simply list can be referred to as the collection of values that we can modify in place.
Let’s look at some examples of lists in python

Example 1 of List: Creating and Printing the List
As the name suggests in this example we will create a list in python and will print it.

Code Implementation

my_list = [1, 2, 3, 4, 5]
print(my_list)

Output

[1, 2, 3, 4, 5]

Explanation of the above code
In the above example we have created a list with the name my_list and that contains five integers. After that we are just printing the list to make sure the list contains the same value as we have assigned.

Example 2 of lists in python: Accessing and Modifying the List Elements
In this example we will see the mutable nature of a list as we will modify the elements of the list.

Code Implementation

fruits = ['apple', 'banana', 'cherry']
print(fruits[1])
fruits[2] = 'orange'
print(fruits)

Output

banana
['apple', 'banana', 'orange']

Explanation of the above example
In the above example we have a list with the name fruits and after that we have changed the third element of the list from cherry to orange and when printing the list we can see that the original list has been changed hence the list is mutable.

Dictionary in Python

This is also one of the most commonly used mutable data types in python. Unlike a list, it is the collection of key-value pairs, where each key maps to a value. Now let’s look at some of the examples of dictionaries in python.

Example 1: Creating and Printing Dictionary in Python
In this example, we are creating a dictionary in python and printing it.

Code Implementation

person = {'name': Naman, 'age': 21, 'city': 'New Delhi'}
print(person)

Output

{'name': 'Naman', 'age': 21, 'city': 'New Delhi'}

Explanation of the above example
In the above example we have created a dictionary named person which contains the three key-value pair that is name age and location of the person.
After creating the dictionary we are printing it to make sure these are the same as we have assigned them during initialization.

Example 2: Accessing and Modifying the Value in the Dictionary
As we have already mentioned that dictionaries in python are mutable data types. So here we will change the value of a key-value pair and will see whether the changes are reflected or not.

Code Implementation

person = {'name': 'Naman', 'age': 21, 'city': 'New Delhi'}
print(person['name'])
person['age'] = 25
print(person)

Output

Naman
{'name': 'Naman', 'age': 25, 'city': 'New Delhi'}

Explanation of the above example
In the above example we have created a list with three key-value pairs and after that, we have changed a value of age and after that, we are printing the dictionary and in the output, we can see that the changes are reflected so this proves that the dictionary is mutable.

Sets in Python

This is the last of the mutable data type in python. It is the collection of unique values that can be modified in place. Now, look at various examples of sets in Python.

Example 1: Creating and Printing Set
In this example we are creating a set and then printing it.

Code Implementation

my_set = {1, 2, 3, 4, 5}
print(my_set)

Output

{1, 2, 3, 4, 5}

Explanation of the above example
In the above example we have created a set with the name my_set and then we have printed it in the output to check whether it is same as assigned or declared or not.

Example 2: Performing Operations and adding elements in set
In this example we will see some operations on set.

Code Implementation

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
union = set1.union(set2)
intersection = set1.intersection(set2)
difference = set1.difference(set2)
set1.add(25)
set2.add(59)
print(union)
print(intersection)
print(difference)
print(set1)
print(set2)

Output

{1, 2, 3, 4, 5, 6, 7, 8}
{4, 5}
{1, 2, 3}
{1, 2, 3, 4, 5, 25}
{4, 5, 6, 7, 8, 59}

Explanation of the above example
In the above example we have performed various operations like union, intersection, etc. after that we have added the elements in both the set and in output we can see the added element hence the set is mutable data type in python.

Applications of Mutable Data Types in Python

There are various applications of Mutable data types in python ranging in different domains. Some of the applications are mentioned below:

  • Data manipulation and processing: There is a benefit of using mutable data types in python as we can change the data so they can be used in data manipulation and processing where the data needs to be changed regularly.
  • Software development: They are used in software development and object-oriented programming to be more specific as their mutable nature helps in encapsulating the data and creating a reusable code.
  • Web Programming: Python is widely used in web development and with the help of mutable data types we can create dynamic and interactive web applications.

Conclusion
By summarising the mutable data types are an essential feature of python that will allow the developers to create data structures that can be modified after creation during program execution. Some of the mutable data types are lists, dictionaries, sets. They offer many beinfits like they will enhance the efficiency, convenience, and flexibility. We have also understood various properties of mutable objects in python along with their applications. At last developers use mutable data types in python so that they can create more effective programs.

Frequently Asked Questions

Here are some of the frequently asked questions and answers about mutable data types in python.

1. What is the importance of mutable data types in python?
It will allow the user to modify the data without creating a new object.

2. What is the difference between the append and extend methods in Python?
The append method is used to add a single element to the list whereas the extend method is used to add multiple items to the list.

3. What is the difference between the remove and discard methods in Python?
Both of the methods are used to remove the specific element from the set but the remove will throw an error if the element is not present in the set whereas discard will not throw an error.

4. Can the user modify a list while traversing in python?
Yes the user can change the list.

Leave a Reply

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