Changing Dictionary Items
Dictionary items are mutable; you can change the value of a specific item by referring to its key name.
Modifying Existing Items
Example: Changing a Value
# Changing a dictionary item
person = {"name": "John", "age": 26, "city": "Mumbai"}
person["age"] = 27
print("Updated Person:", person)
Updated Person: {'name': 'John', 'age': 27, 'city': 'Mumbai'}
Updating Multiple Items
Example: Using update()
Method
# Updating multiple dictionary items
person.update({"age": 28, "city": "Delhi"})
print("Person after update:", person)
Person after update: {'name': 'John', 'age': 28, 'city': 'Delhi'}
Explanation: You can change the value of a dictionary item by assigning a new value to its key. The update()
method allows you to update multiple items at once.