dict.__or__
(PEP-584)
There are a lot of ways to merge two dicts:
-
Long but simple:
merged = d1.copy() merged.update(d2)
-
merged = {**d1, **d2}
-
Unpacking again (keys must be strings):
merged = dict(d1, **d2)
-
collections.ChainMap
. Result is notdict
but so.
In python 3.9, PEP-584 introduced the 5th way. Meet the |
operator for dict
!
merged = d1 | d2
Basically, that is the same as the first way but shorter and can be inlined.