Skip to content

Instantly share code, notes, and snippets.

@chansuke
Created August 19, 2014 05:16
Show Gist options
  • Save chansuke/9737cdca87d6cff9691d to your computer and use it in GitHub Desktop.
Save chansuke/9737cdca87d6cff9691d to your computer and use it in GitHub Desktop.
Check iO - The Flat Dictionary
def flatten(dictionary):
stack = [((), dictionary)]
result = {}
while stack:
path, current = stack.pop()
for k, v in current.items():
if isinstance(v, dict):
stack.append((path + (k,), v))
else:
result["/".join((path + (k,)))] = v
if len(current) == 0:
result["/".join(path)] = ""
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment