+def coalesce_by_creating_list(key, v1, v2):
+ from list_utils import flatten
+ return flatten([v1, v2])
+
+
+def coalesce_by_creating_set(key, v1, v2):
+ return set(coalesce_by_creating_list(key, v1, v2))
+
+
+def raise_on_duplicated_keys(key, v1, v2):
+ raise Exception(f'Key {key} is duplicated in more than one input dict.')
+
+
+def coalesce(
+ inputs: Iterator[Dict[Any, Any]],
+ *,
+ aggregation_function: Callable[[Any, Any], Any] = coalesce_by_creating_list
+) -> Dict[Any, Any]:
+ """Merge N dicts into one dict containing the union of all keys/values in
+ the input dicts. When keys collide, apply the aggregation_function which,
+ by default, creates a list of values. See also coalesce_by_creating_set or
+ provide a user defined aggregation_function.
+
+ >>> a = {'a': 1, 'b': 2}
+ >>> b = {'b': 1, 'c': 2, 'd': 3}
+ >>> c = {'c': 1, 'd': 2}
+ >>> coalesce([a, b, c])
+ {'a': 1, 'b': [1, 2], 'c': [1, 2], 'd': [2, 3]}
+
+ """
+ out: Dict[Any, Any] = {}
+ for d in inputs:
+ for key in d:
+ if key in out:
+ value = aggregation_function(key, d[key], out[key])
+ else:
+ value = d[key]
+ out[key] = value
+ return out
+
+
+def item_with_max_value(d: Dict[Any, Any]) -> Tuple[Any, Any]:
+ """Returns the key and value with the max value in a dict.
+
+ >>> d = {'a': 1, 'b': 2, 'c': 3}
+ >>> item_with_max_value(d)
+ ('c', 3)
+ >>> item_with_max_value({})
+ Traceback (most recent call last):
+ ...
+ ValueError: max() arg is an empty sequence
+
+ """