spekk.trees.core.merge

Contents

spekk.trees.core.merge#

spekk.trees.core.merge(t1: Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any, t2: Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any, preserve_order: int = 'first') Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any[source]#

Merge two trees (assuming this is possible).

The order of the keys in the merged tree is determined by the preserve_order. If it is first, then the order is the same as the first tree. If it is last, then the order is the same as the second tree. Any other new keys are appended to the end of the tree.

>>> merge({"a": 1, "b": 2}, {"c": 3})
{'a': 1, 'b': 2, 'c': 3}
>>> merge({"a": 1, "b": 2}, {"b": 7, "c": 3})
{'a': 1, 'b': 7, 'c': 3}

Merging two lists treats the indices as keys, and the second tree overwrites the indices of the first one.

>>> merge([1, 2, 3], [4, 5])
[4, 5, 3]