Skip to content

Instantly share code, notes, and snippets.

@ericstone57
Last active October 26, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericstone57/fb3b709c6f8eeb3f6ebd8e85d923842f to your computer and use it in GitHub Desktop.
Save ericstone57/fb3b709c6f8eeb3f6ebd8e85d923842f to your computer and use it in GitHub Desktop.
to flatten dict strcuture
def dict_flatten(d, parent_key='', sep=''):
items = []
for k, v in d.items():
new_key = parent_key + sep + str(k) if parent_key else str(k)
if v and isinstance(v, collections.MutableMapping):
items.extend(dict_flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment