Open In App

Are Dictionaries Mutable or Immutable in Python

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, dictionaries are mutable. This means that you can modify the contents of a dictionary after it has been created. You can add, update or remove key-value pairs in a dictionary which makes it a flexible and dynamic data structure.

What Does It Mean for Dictionaries to Be Mutable?

Being mutable means that the state of the dictionary can change over time. You can perform several operations that modify the dictionary without needing to create a new one.

The most common operation that demonstrate the mutability of dictionaries in Python is :

Adding Key-Value Pairs

You can add new key-value pairs to an existing dictionary:

Python
# Creating a dictionary
dict = {'a': 1, 'b': 2}

# Adding a new key-value pair
dict['c'] = 3

print(dict)  

Output
{'a': 1, 'b': 2, 'c': 3}

In this case, we added a new key 'c' with the value 3 to the dictionary.

Since, dictionary is mutable, we can perform more operations on dictionary like


Next Article
Article Tags :
Practice Tags :

Similar Reads