It seems that dict can be merged from Python 3.9.
You can do it with |
.
Python 3.9.0b5 (default, Aug 6 2020, 10:04:28)
[Clang 12.0.0 (clang-1200.0.26.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {'a1': 1}
>>> b = {'b1': 1}
>>> a | b
{'a1': 1, 'b1': 1}
>>> a = {'a1': 1}
>>> b = {'b1': 1}
>>> a |= b
>>> a
{'a1': 1, 'b1': 1}
>>> a1 = {'a': 1, 'b': 1}
>>> a2 = {'a': 2, 'b': 2, 'c': 2}
>>> a1 | a2
{'a': 2, 'b': 2, 'c': 2}
>>> a2 | a1
{'a': 1, 'b': 1, 'c': 2}
>>> d1 = {'a': 1, 'b': {'c': 2}}
>>> d2 = {'z': 999}
>>> d1 | d2
{'a': 1, 'b': {'c': 2}, 'z': 999}
>>> d1 = {'a': 1, 'b': {'c': 2}}
>>> d2 = {'z': 999, 'b': {'c': 888}}
>>> d1 | d2
{'a': 1, 'b': {'c': 888}, 'z': 999}
I've only tried it very easily, but it seems convenient
Recommended Posts