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
Copy Code
dictionary.update(dict[, **pairs])
dictionary.update(iterable[, **pairs])
Parameters
Parameter | Description |
---|---|
dict |
Optional. A dictionary of key/value pairs that will be used to update the given
dictionary. The dictionary is updated using
Python Copy Code 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 Copy Code 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 Copy Code for key in pairs: dictionary[key] = pairs[key] |
Example
Python
Copy Code
fish = { 'guppy': 2, 'zebra' : 5, 'betta': 10 } fish.update({'guppy': 3, 'shark': 1}) print(fish)
Output
{'guppy': 3, 'zebra': 5, 'betta': 10, 'shark': 1}