Last active
November 14, 2020 00:26
-
-
Save mosajjal/95500394e357858e984298c100647ecc to your computer and use it in GitHub Desktop.
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 flatten_dict(in_, joiner="."): | |
out = dict() | |
retry = 0 | |
for item in in_: | |
if type(in_[item]) == list: | |
retry = 1 | |
for i,list_item in enumerate(in_[item]): | |
out[f"{item}{joiner}{i}"] = in_[item][i] | |
elif type(in_[item]) == dict: | |
retry = 1 | |
for dict_item in in_[item]: | |
out[f"{item}{joiner}{dict_item}"] = in_[item][dict_item] | |
else: | |
out[item] = in_[item] | |
if retry: | |
out=flatten_dict(out, joiner=joiner) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment