Search notes:
Python: merge two dictionaries
The following example tries to demonstrate how two
dictionaries can be merged.
dict_1 = {'x' : 'eggs', 'y' : 'why' , 'num': 99}
dict_2 = {'num': 42 , 'hello': 'world' }
dict_merged = {**dict_1, **dict_2}
for k, v in dict_merged.items():
print(f"{k} = {v}")
When executed, this script prints:
x = eggs
y = why
num = 42
hello = world
Note, the value of the num
key of dict_2
has overwritten the corresponding value of dict_1
.
See also
The
update()
method of a
dict
updates a
dict
with the key/value pairs of another
dict
.