Junior — Middle
Is it possible to modify the contents of a dictionary after its creation?
sobes.tech AI
Answer from AI
Yes, the contents of a dictionary in Python can be modified after its creation. A dictionary is a mutable data type, so you can add new key-value pairs, modify the values of existing keys, or delete elements.
Example:
my_dict = {'a': 1, 'b': 2}
# Adding a new element
my_dict['c'] = 3
# Modifying the value for a key
my_dict['a'] = 10
# Deleting an element
del my_dict['b']
print(my_dict) # Output: {'a': 10, 'c': 3}