Created
April 11, 2012 13:47
-
-
Save jfcamel/2359399 to your computer and use it in GitHub Desktop.
python dictionary flatten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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