Get Mystery Box with random crypto!

#Python factlet: Dictionary union x |= y matches x.update(y) a | Nikita Shpilevoy | About IT, AI, etc

#Python factlet: Dictionary union x |= y matches x.update(y) and keeps order of x with overriding by y values and adding new key/value pairs. Like {**x, **y).

{**d1, **d2} looks uggly and ignores the types of the mappings and always returns a dict.

Python 3.10 introduced | operator for dict union:
>> a = {'x1': 1, 'x2': 2}
>> b = {'x2': 3, 'x3': 4}
>> a | b
{'x1': 1, 'x2': 3, 'x3': 4}

ChainMap is the way to resolve ordering by favoring the "deepest" dicts over the "shallowest":

>>> dict(ChainMap(dict(z=6, x=7), dict(q=1, w=2, y=3, r=4, d=5)))
{'q': 1, 'w': 2, 'z': 6, 'r': 4, 'd': 5, 'x': 7}

For example, the good use case for ChainMap is where root dict defines keys in some order you want to keep and "shallower" dict is just something you want to override root dict.

Check PEP 585 if you are interested in details and motivation of these operators.