Are Dictionaries Mutable in Python : Quick Guide

Are Dictionaries Mutable in Python featured image

Python is the most used programming language. It offers a wide range of data structures that allow you to store and manipulate data effectively. A dictionary is one of them. But there is a question arisen for many coders that are Are Dictionaries Mutable in Python or not? In this tutorial, I will demonstrate whether Are dictionaries Mutable in Python or not.

What is a dictionary in Python?

The dictionary is a data structure that allows you to store multiple values using the key-value pair. All the key-value pairs are inside the curly brackets {}. All the elements inside it are separated by a comma.

Are Dictionaries Mutable in Python?

Before knowing the mutability of the dictionary let’s understand what is Mutable. In Python programming, mutability means once the object is defined or created, you can modify it whenever you want. Its opposite is immutable. It means once the object is defined cannot be changed.

The answer of the question “Are Dictionaries Mutable in Python “? The answer is yes. Dictionaries are mutable in Python. You can easily modify the contents of the dictionary after it has been created. You can add, remove, and update key-value pairs of a dictionary.

Let us understand it using the examples.

Example 1: Adding Key-Value Pairs

In this example, you can easily add new key-value pair inside the dictionary using the asignment operator.

Execute the below lines of code and see the output.

my_dict = {'name': 'Abhishek', 'age': 29}
my_dict['city'] = 'Delhi'
print(my_dict)

In the above code, I have added the “city”:”Delhi” key-value to the existing my_dict dictionary.

Output

Add new key value pair in existing dictionary
Add new key value pair in existing dictionary

Example 2: Updating Values of the dictionary

The second example that proves that a dictionary is mutable is to update or change the value of a particular key of the dictionary. For example, lets I want to change the name from “Abhishek” to “Sukesh” then I will use the below lines of code.

my_dict = {'name': 'Abhishek', 'age': 29}
my_dict['name'] = 'Sukesh'
print(my_dict)

Output

updating the value using the key
Updating the value using the key

Example 3: Key-value pair removal

In this example, I will remove the name key-value pair. To do so you will use the del keyword with the key in the square bracket.

Run the below lines of code to remove it

my_dict = {'name': 'Abhishek', 'age': 29}
del my_dict['name']
print(my_dict)

Output

removing the name key-value pair
removing the name key-value pair

Conclusion

All the above examples have proved that the dictionary in Python is mutable. In fact You can easily add new key-value pairs, modify the existing keys, and remove a particular key-value pair.

I hope this post has answered your query “Are Dictionaries Mutable in Python”? If you have any other queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner