Python's Dict Union Operator | Pep 584


Today I was watching the python web conf 2022 and saw @davidbujic use the new Dict Union Operator Live on stage during his Functional Programming talk. This operator was first introduced into python 3.9 with pep584.

Merge Dicts

I've long updated dicts through the use of unpacking. Note that the last item always wins. It makes it pretty easy to make user overrides to default configurations. With pep584 landing in python 3.9 we can now leverage the | operator to achieve the same result.

default_config = {'url': '', 'assets_dir': 'static' }
user_config = {'url': ''}

# **unpacking goes back much further than 3.9

config = {**default_config, **user_config}
# {'url': '', 'assets_dir': 'static'}

# the same can be achieved through the new to python 3.9 | operator

config = default_config | user_config
# {'url': '', 'assets_dir': 'static'}

More on unpacking in this post.

Update Dicts

With the release there is also a new update syntax |= that you can use to update. I dont often mutate variables for some reason, so I cant think of a better example for this from my personal use cases. So I will give a similar example to above, except creating a config, then updating it.

# old python <3.9 way
config = {'url': '', 'assets_dir': 'static' }
config.update({'url': ''})

# new python 3.9+ way
config = {'url': '', 'assets_dir': 'static' }
config |= {'url': ''}

# {'url': '', 'assets_dir': 'static'}

Should you use it?

Are you writing libraries/applications that are only going to be ran on 3.9? Then ya go for it there is nothing to loose. If there is any chance someone is going to run your code on 3.8 or older then just use **, or .update.


This is what comes first to my mind on how to use this new syntax, read pep584 for all the gritty details on it.