KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Science

Why are dictionaries called mutable types?

Python Dictionaries

6 Likes

Answer

Dictionaries can be changed by adding new key-value pairs and by deleting or changing the existing ones. Hence they are called as mutable types.
For example:

d = {"a" : 1 , "b" : 2}
d["c"] = 3 
d["b"] = 4
del d["a"]
print(d)
Output
{'b': 4, 'c': 3} 

dict["c"] = 3 adds a new key-value pair to dict.
dict["b"] = 4 changes existing key-value pair in dict.
del dict["a"] removes the key-value pair "a" : 1

Answered By

2 Likes


Related Questions