Skip to main content
Don't really need the "multiple unions" part
Source Link
xjcl
  • 14.4k
  • 7
  • 76
  • 99

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 9, 'c': 3}
>>> d | e
{'a': 9, 'b': 2, 'c': 3}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 3, 'b': 2}

Multiple unions per line are also possible:

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

Could be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 9, 'c': 3}
>>> d | e
{'a': 9, 'b': 2, 'c': 3}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 3, 'b': 2}

Multiple unions per line are also possible:

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

Could be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 9, 'c': 3}
>>> d | e
{'a': 9, 'b': 2, 'c': 3}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 3, 'b': 2}
improve clarity of examples
Source Link
xjcl
  • 14.4k
  • 7
  • 76
  • 99

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 49, 'c': 63}
>>> d | e
{'a': 49, 'b': 2, 'c': 63}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 63, 'b': 2}

Multiple unions per line are also possible:

>>> {1 'a': 21} | {3 'b': 42} | {5 'c': 63}
{1'a': 21, 3'b': 42, 5'c': 63}

Could be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 4, 'c': 6}
>>> d | e
{'a': 4, 'b': 2, 'c': 6}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 6, 'b': 2}

Multiple unions per line are also possible:

>>> {1 : 2} | {3 : 4} | {5 : 6}
{1: 2, 3: 4, 5: 6}

Could be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 9, 'c': 3}
>>> d | e
{'a': 9, 'b': 2, 'c': 3}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 3, 'b': 2}

Multiple unions per line are also possible:

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

Could be useful for something like module_defaults | user_defaults | user_arguments.

added 2 characters in body
Source Link
xjcl
  • 14.4k
  • 7
  • 76
  • 99

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 4, 'c': 6}
>>> d | e
{'a': 4, 'b': 2, 'c': 6}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 6, 'b': 2}

Multiple unions per line are also possible:

>>> {1 : 2} | {3 : 4} | {5 : 6}
{1: 2, 3: 4, 5: 6}

This couldCould be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 4, 'c': 6}
>>> d | e
{'a': 4, 'b': 2, 'c': 6}

For matching keys, the right dict takes precedence

This also works for |= to modify a dict in-place

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 6, 'b': 2}

Multiple unions per line are also possible:

>>> {1 : 2} | {3 : 4} | {5 : 6}
{1: 2, 3: 4, 5: 6}

This could be useful for something like module_defaults | user_defaults | user_arguments.

New in Python 3.9: Use the union operator (|) to merge dicts similar to sets:

>>> d = {'a': 1, 'b': 2}
>>> e = {'a': 4, 'c': 6}
>>> d | e
{'a': 4, 'b': 2, 'c': 6}

For matching keys, the right dict takes precedence.

This also works for |= to modify a dict in-place:

>>> e |= d    # e = e | d
>>> e
{'a': 1, 'c': 6, 'b': 2}

Multiple unions per line are also possible:

>>> {1 : 2} | {3 : 4} | {5 : 6}
{1: 2, 3: 4, 5: 6}

Could be useful for something like module_defaults | user_defaults | user_arguments.

Source Link
xjcl
  • 14.4k
  • 7
  • 76
  • 99
Loading