flattendict#

flattendict(nesteddict, sep=None, _prefix=None)[source]#

Flatten nested dictionary

Parameters:
  • nesteddict (dict) – the dictionary to flatten

  • sep (str) – the separator used to separate keys

Example:

>>> sc.flattendict({'a':{'b':1,'c':{'d':2,'e':3}}})
{('a', 'b'): 1, ('a', 'c', 'd'): 2, ('a', 'c', 'e'): 3}
>>> sc.flattendict({'a':{'b':1,'c':{'d':2,'e':3}}}, sep='_')
{'a_b': 1, 'a_c_d': 2, 'a_c_e': 3}
Parameters:
  • nesteddict (dict) – Input dictionary potentially containing dicts as values

  • sep (str) – Concatenate keys using string separator. If None the returned dictionary will have tuples as keys

  • _prefix – Internal argument for recursively accumulating the nested keys

Returns:

A flat dictionary where no values are dicts

New in version 2.0.0: handle non-string keys.