Skip to content

Instantly share code, notes, and snippets.

@mosajjal
Last active November 14, 2020 00:26
Show Gist options
  • Save mosajjal/95500394e357858e984298c100647ecc to your computer and use it in GitHub Desktop.
Save mosajjal/95500394e357858e984298c100647ecc to your computer and use it in GitHub Desktop.
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