- update() python function of dictionary are able to update the value of the given key.
Syntax-
dict.update(arg)
- Here arg is the pair of keys and values in dictionary form.
Example-
dictionary is d={1:'a',2:'b'}
d.update({1:'z'}) will return {1:'z',2:'b'}
dictionay is d={1:'a',2:'b'}
d.update({3:'y'}) will return {1:'a',2:'b',3:'y'} // that means it create new key if previous not found.
program-
d={1:"pre",2:'b'}
print(d)
d.update({1:'next1'})
print(d)
d.update({2:'next2'})
print(d)
d.update({3:'pre'})
print(d)
d.update({3:'next'})
print(d)
{1: 'pre', 2: 'b'}
{1: 'next1', 2: 'b'}
{1: 'next1', 2: 'next2'}
{1: 'next1', 2: 'next2', 3: 'pre'}
{1: 'next1', 2: 'next2', 3: 'next'}