Home / Python / Reference / Dictionary methods

Python dictionary update() method

Add or overwrite pairs from another dictionary.

Syntax

dict.update(other)

Keys that exist get the new value; new keys are added. a | b makes a new merged dictionary instead of changing a.

Parameters

othera dict, pairs, or key=value arguments

Return value

None

Example

Code

settings = {"theme": "light", "size": 12}
settings.update({"theme": "dark", "lang": "en"})
print(settings)
settings.update(size=14)
print(settings)
print({"a": 1} | {"b": 2})

Output

{'theme': 'dark', 'size': 12, 'lang': 'en'}
{'theme': 'dark', 'size': 14, 'lang': 'en'}
{'a': 1, 'b': 2}

Related

Learn it step by step in the free lesson Dictionaries & Sets, with exercises that check your answers.