Python 3.9 dict merge (`|`) seems to be useful

Overview

It seems that dict can be merged from Python 3.9. You can do it with |.

Python used

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.

I'll try

Basic usage

>>> a = {'a1': 1}
>>> b = {'b1': 1}
>>> a | b
{'a1': 1, 'b1': 1}

Merge and assign

>>> a = {'a1': 1}
>>> b = {'b1': 1}
>>> a |= b
>>> a
{'a1': 1, 'b1': 1}

When there is the same key

>>> 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}

A little complicated dict

>>> 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}

Summary

I've only tried it very easily, but it seems convenient

Recommended Posts

Python 3.9 dict merge (`|`) seems to be useful
[Python] There seems to be something called __dict__
[Road to Intermediate] Python seems to be all objects
[Python] It might be useful to list the data frames
Useful to remember! 10 Python Standard Libraries
Foxit's Heartbleed inspection script seems to be useful for general-purpose packet generation
Python has come to be recognized so far.
Updated to Python 2.7.9
I want to merge nested dicts in Python
Python Dict category
I tried to implement what seems to be a Windows snipping tool in Python
"Backport" to python 2
Starting with Python 3.10, the form returned by inspect.signature () seems to be based on typing.get_type_hints ().
[Python] tkinter Code that is likely to be reused
[Python] How to sort dict in list and instance in list
[Python] pandas Code that is likely to be reused
Only size-1 arrays can be converted to Python scalars
Convert Webpay Entity type to Dict type (recursively in Python)
python> Is it possible to make in-line comments?> It seems that it has to be on multiple lines
Changes from Python 3.0 to Python 3.5
[Python] Super useful debugging
How to install python
python decorator to retry
Introduction to Python language
Introduction to OpenCV (python)-(2)
Convert dict to array
Note to daemonize python
What seems to be a template of the standard input part of the competition pro in python3
Introducing Python 2.7 to CentOS 6.6
Merge Nodes-Houdini Python Practice
Connect python to mysql
[Python MinMaxScaler] Normalize to 0 ~ 1
python useful memo links
I want to be able to analyze data with Python (Part 3)
How to switch the configuration file to be read by Python
Try to display various information useful for debugging with python
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
Five useful Python data types that are easy to forget
I want to be able to analyze data with Python (Part 2)
Useful tricks related to list and for statements in Python
Note links that may be useful when using Python, Selenium2
I want to be able to run Python in VS Code
How doi may be useful when asking how to write code?
[Python] Allow pip3 packages to be imported on AWS Lambda
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments