mlnext.utils.flatten#
- mlnext.utils.flatten(mapping: Mapping[str, Any], *, prefix: str = '', sep: str = '.', flatten_list: bool = True) Mapping[str, Any] [source]#
Turns a nested mapping into a flattened mapping.
- Parameters:
mapping (T.Mapping[str, T.Any]) – Mapping to flatten.
prefix (str) – Prefix to preprend to the key.
sep (str) – Seperator of flattened keys.
flatten_list (bool) – Whether to flatten lists.
- Returns:
Returns a (flattened) mapping.
- Return type:
T.Mapping[str, T.Any]
Example
>>> flatten({ ... 'flat1': 1, ... 'dict1': {'c': 1, 'd': 2}, ... 'nested': {'e': {'c': 1, 'd': 2}, 'd': 2}, ... 'list1': [1, 2], ... 'nested_list': [{'1': 1}] ... }) { 'flat1': 1, 'dict1.c': 1, 'dict1.d': 2, 'nested.e.c': 1, 'nested.e.d': 2, 'nested.d': 2, 'list1.0': 1, 'list1.1': 2, 'nested_list.0.1': 1 }