Skip to content

Is dictionary mutable in Python?

  • by

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 or suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading