EN
Python - update dictionary items
3 points
In this article, we would like to show you how to update dictionary items in Python.
In this example, we use update()
method to change the value of the existing item.
xxxxxxxxxx
1
my_dict = {
2
"id": 1,
3
"name": "Tom",
4
"age": 25
5
}
6
7
my_dict.update({"age": 30})
8
9
print(my_dict.items()) # [('id', 1), ('name', 'Tom'), ('age', 30)]
Output:
xxxxxxxxxx
1
dict_items([('id', 1), ('name', 'Tom'), ('age', 30)])
Note:
If the item doesn't exist, the
update()
method will add it to the dictionary.