Python Dictionary update() Method

Updates the dictionary with the specified key-value pairs. If the key exists, the value will be updated. If the key does not exist, the value will be inserted using the given key.

Syntax

Python
dictionary.update(dict[, **pairs])
dictionary.update(iterable[, **pairs])    

Parameters

ParameterDescription
dict Optional. A dictionary of key/value pairs that will be used to update the given dictionary. The dictionary is updated using
Python
for key in dict:
    dictionary[key] = dict[key]    
iterable Optional. An iterable of key/value pairs that will be used to update the given dictionary. The dictionary is updated using
Python
for key, value in iterable:
    dictionary[key] = value    
pairs Optional. An iterable of key/value pairs. If present, the dictionary will be updated using
Python
for key in pairs:
    dictionary[key] = pairs[key]

Example

Python
fish = {
    'guppy': 2,
    'zebra' : 5,
    'betta': 10
}

fish.update({'guppy': 3, 'shark': 1})
print(fish)

Output

{'guppy': 3, 'zebra': 5, 'betta': 10, 'shark': 1}