Last active
October 26, 2020 18:52
-
-
Save ericstone57/fb3b709c6f8eeb3f6ebd8e85d923842f to your computer and use it in GitHub Desktop.
to flatten dict strcuture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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