Skip to content

Instantly share code, notes, and snippets.

@BibMartin
Last active May 9, 2017 07:37
Show Gist options
  • Select an option

  • Save BibMartin/4fca400c9916f541fbfcc4045cfd5125 to your computer and use it in GitHub Desktop.

Select an option

Save BibMartin/4fca400c9916f541fbfcc4045cfd5125 to your computer and use it in GitHub Desktop.
def unstack(x, prefix=""):
if isinstance(x, dict):
out = {}
for key, val in x.items():
z = unstack(val, prefix=key+'.')
if isinstance(z, dict):
for subkey, subval in z.items():
out[prefix+subkey] = subval
else:
out[prefix+key] = val
return out
elif isinstance(x, list):
return unstack({str(i): val for i,val in enumerate(x)}, prefix=prefix)
else:
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment