Skip to content

Instantly share code, notes, and snippets.

@mastier mastier/flatten_dict.py
Last active Dec 14, 2015

Embed
What would you like to do?
# useful way to flatten dictionary representing i.e. path to file and content
# now fixed in case of running it again
def flatten_dict(d,path=(),paths=None, sep='/'):
if paths==None: paths=[]
if isinstance(d,dict):
for k,v in d.items():
flatten_dict(v,path+(k,),paths)
return paths
else:
paths.append( { sep+sep.join(path) : d } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.