Python etc / `dict.__or__` (PEP-584)

dict.__or__ (PEP-584)

There are a lot of ways to merge two dicts:

  1. Long but simple:

    merged = d1.copy()
    merged.update(d2)
    
  2. Unpacking:

    merged = {**d1, **d2}
    
  3. Unpacking again (keys must be strings):

    merged = dict(d1, **d2)
    
  4. collections.ChainMap. Result is not dict 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.