flatten

  • full name: tenpy.tools.misc.flatten

  • parent module: tenpy.tools.misc

  • type: function

tenpy.tools.misc.flatten(mapping, separator='.')[source]

Obtain a flat dictionary with all key/value pairs of a nested data structure.

Parameters:

separator (str) – Separator for merging keys to a single string.

Returns:

flat_config – A single dictionary with all key-value pairs.

Return type:

dict

Examples

>>> sample_data = {'some': {'nested': {'entry': 100, 'structure': 200},
...                         'subkey': 10},
...                'topentry': 1}
>>> flat = flatten(sample_data)
>>> for k in sorted(flat):
...     print(repr(k), ':', flat[k])
'some.nested.entry' : 100
'some.nested.structure' : 200
'some.subkey' : 10
'topentry' : 1

See also

get_recursive

Useful to obtain a single entry from a nested data structure.