Skip to content

Instantly share code, notes, and snippets.

/flatten Secret

Created July 3, 2014 15:07
Show Gist options
  • Save anonymous/af0980414f5b304a9351 to your computer and use it in GitHub Desktop.
Save anonymous/af0980414f5b304a9351 to your computer and use it in GitHub Desktop.
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
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment