Skip to content

Instantly share code, notes, and snippets.

@mehdidc
Created October 15, 2016 21:01
Show Gist options
  • Save mehdidc/cd02ab212c37afe59b4cb80ae4db2c11 to your computer and use it in GitHub Desktop.
Save mehdidc/cd02ab212c37afe59b4cb80ae4db2c11 to your computer and use it in GitHub Desktop.
import collections
def flatten_dict(l):
d = {}
for k, v in l.items():
if isinstance(v, collections.Mapping):
d.update(flatten_dict(v))
elif isinstance(v, list) or isinstance(v, tuple):
for i, l in enumerate(v):
d[k+'_{}'.format(i)] = l
else:
d[k] = v
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment