Is dictionary mutable in Python?

Python dictionary is a mutable data structure. The mutable data type allows entries, removed, and changed values at any time.

Example check Python dictionary mutable

Simple example code if dictionaries are mutable in Python then we can add, delete, and changed the data values even after creating.

Here are examples of how to add, delete, or change the data entries of the dictionary:

# Create a dictionary
grades = {'A': 1, 'B': 2, 'C': 3}

grades['A'] = 99  # Change the value of A

grades['D'] = 4  # adds a new entry

del grades['B']  # remove B

print(grades)

Output: If the dictionary is immutable then TypeError will throw by the compiler.

Is dictionary mutable in Python

Do comment if you have any doubts and suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.