The update()
method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Example
marks = {'Physics':67, 'Maths':87}
internal_marks = {'Practical':48}
marks.update(internal_marks)
print(marks)
# Output: {'Physics': 67, 'Maths': 87, 'Practical': 48}
Syntax of Dictionary update()
The syntax of update()
is:
dict.update([other])
update() Parameters
The update()
method takes either a dictionary or an iterable object of key/value pairs (generally tuples).
If update()
is called without passing parameters, the dictionary remains unchanged.
Return Value from update()
update()
method updates the dictionary with elements from a dictionary object or an iterable object of key/value pairs.
It doesn't return any value (returns None
).
Example 1: Working of update()
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2
d.update(d1)
print(d)
d1 = {3: "three"}
# adds element with key 3
d.update(d1)
print(d)
Output
{1: 'one', 2: 'two'} {1: 'one', 2: 'two', 3: 'three'}
Note: The update()
method adds element(s) to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value.
Example 2: update() When Tuple is Passed
dictionary = {'x': 2}
dictionary.update([('y', 3), ('z', 0)])
print(dictionary)
Output
{'x': 2, 'y': 3, 'z': 0}
Here, we have passed a list of tuples [('y', 3), ('z', 0)]
to the update()
function. In this case, the first element of tuple is used as the key and the second element is used as the value.
Also Read: