combine

combine(data: dict, levels: tuple[int, int], delim: str = '-') dict[source][source]

Combine any levels of a nested dict into the lower level

Takes the input nested dict and rearranges the top and bottom sub-dictionary.

Parameters:
  • data (dict) – The nested dict to combine

  • levels (tuple[int, int]) – The levels to combine, e.g. (0, 1) will combine the 1st and 2nd level of the dict keys into one level at the 2nd level.

  • delim (str, optional) – The delimiter to use when combining keys, by default ‘-’

Returns:

The combined dict

Return type:

dict

Examples

>>> data = {'a': {'b': {'c': 1}}}
>>> combine(data, (0, 2))
{'b': {'a-c': 1}}
>>> data = {'a': {'b': {'c': 1}}, 'd': {'b': {'c': 2, 'e': 3}}}
>>> combine(data, (0, 2))
{'b': {'a-c': 1, 'd-c': 2, 'd-e': 3}}