merge_dicts (or |)

New in Atfinity 17.

Description

merge_dicts merges any number of dictionaries into a single dictionary. It is variadic, so you can pass two, three, or more dictionaries. For merging exactly two dictionaries, the infix | operator can be used as a shorter alternative.

When the same key appears in more than one dictionary, the value from the later argument (or, for |, the right-hand side) wins. This matches the semantics of Python's dict | dict operator.

Examples

merge_dicts({'a': 1}, {'b': 2})
// => {'a': 1, 'b': 2}

Later values overwrite earlier ones on overlap:

merge_dicts({'a': 1}, {'b': 2}, {'a': 99})
// => {'a': 99, 'b': 2}

Using the infix | operator:

{'a': 1} | {'b': 2}
// => {'a': 1, 'b': 2}

{'a': 1, 'b': 2} | {'b': 99}
// => {'a': 1, 'b': 99}

Last updated

Was this helpful?