Skip to content

Instantly share code, notes, and snippets.

@jfcamel
Created April 11, 2012 13:47
Show Gist options
  • Select an option

  • Save jfcamel/2359399 to your computer and use it in GitHub Desktop.

Select an option

Save jfcamel/2359399 to your computer and use it in GitHub Desktop.
python dictionary flatten
def flatten(l):
if isinstance(l, list):
if not l:
return []
else:
return flatten(l[0]) + flatten(l[1:])
return [l]
def dict_flatten(d):
def dict_to_list(d):
if not d:
return []
key_list = list(d)
if not key_list:
return []
else:
return key_list + [dict_flatten(d[k]) for k in key_list]
return flatten(dict_to_list(d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment