Search on blog:

Python: How to merge and update dictionary without changing old dictionary? New method in Python 3.9.

In Python 3.9 there is new method to create new dictionary with updated values and keep old dictionary without updates - using operator |

new_dict = old_dict | dict_with_updates

With list comprehension it will be

arg = [ d | {"a": i} for i in c]

Full example

    d = {"a": 1, "b": 10}
    c = [3, 4, 5]

    arg = [ d | {"a": i} for i in c]


BTW: There is also |= to update existing dictionary

    old_dict |= dict_with_updates

Doc: What’s New In Python 3.9

In older version you can do

Instead of |=


Instead of |

    new_dict = {**old_dict, **dict_with_updates}
If you like it
Buy a Coffee