1 year

Recent Python Language Additions

The majority of the changes are related to Python's standard library, the CPython interpreter, the Python API, and CPython's C API.

In terms of the two latest versions of Python, we can distinguish four main syntax updates:

• Dictionary and merge update operators (added in Python 3.9)

• Assignment expressions (added in Python 3.8)

• Type hinting generics (added in Python 3.9)

• Positional-only arguments (added in Python 3.8)

• The zoneinfo module for supporting the IANA (Internet Assigned Numbers Authority) time zone database (added in Python 3.9)

• The graphlib module for operating with graph-like structures (added in Python 3.8)


Dictionary merge and update operators

Python allows the use of a number of selected arithmetic operators to manipulate the built-in container types, including lists, tuples, sets, and dictionaries.

When it comes to sets, there are exactly four binary operators (having two operands) that produce a new set:

• Intersection operator: Represented by & (bitwise AND). This produces a set with elements common to both sets.

• Union operator: Represented by | (bitwise OR). This produces a set of all elements in both sets.

• Difference operator: Represented by - (subtraction). This produces a set with elements in the left-hand set that are not in the right-hand set.

• Symmetric difference: Represented by ^ (bitwise XOR). This produces a set with elements of both sets that are in either of the sets but not both.


In order to merge two dictionaries into a new dictionary, use the following expression:

dictionary_1 | dictionary_2




>>> {'a': 1} | {'a': 3, 'b': 2}                                                                   

      {'a': 3, 'b': 2}                                                                                  



If you prefer to update the dictionary variable with the keys coming from a different dictionary, you can use the following in-place operator:


existing_dictionary |= other_dictionary




>>> mydict = {'a': 1}                                                                        

>>> mydict |= {'a': 3, 'b': 2}                                                              

>>> mydict {'a': 3, 'b': 2}                                                                  




Dictionary unpacking

Support for dictionary unpacking in dict literals was introduced in Python 3.5 with PEP 448 Additional Unpacking Generalizations.

{**dictionary_1, **dictionary_2}

>>> a = {'a': 1}; b = {'a':3, 'b': 2}                                             

>>> {**a, **b}                                                                         

{'a': 3, 'b': 2}                                                                           


For older versions of Python, it is sometimes better to use a temporary dictionary and a simple update() method.

ChainMap from the collections module

This is a wrapper class that takes multiple mapping objects (dictionaries in this instance) and acts as if it was a single mapping object.

The syntax for merging two dictionaries with ChainMap is as follows:

new_map = ChainMap(dictionary_2, dictionary_1)

Note that the order of dictionaries is reversed compared to the | operator. This means that if you try to access a specific key of the new_map object, it will perform lookups over wrapped objects in left-to-right order.

>>> from collections import ChainMap                                                                 

>>> user_account = {"iban": "GB71BARC20031885581746", "type": "account"} 

>>> user_profile = {"display_name": "John Doe", "type": "profile"}                       

>>> user = ChainMap(user_account, user_profile)                                              

>>> user["iban"] 'GB71BARC20031885581746'                                                   

>>> user["display_name"] 'John Doe'                                                                   

>>> user["type"] 'account'                                                                                     


ChainMap is a wrapper object. This means that it doesn't copy the contents of the source mappings provided, but stores them as a reference. This means that if underlying objects change, ChainMap will be able to return modified data.


ChainMap works with any mapping type. So, if you need to provide unified access to multiple objects that act as if they were dictionaries, ChainMap will enable the provision of a single merge-like unit to do so.