Created
December 11, 2015 13:13
-
-
Save JokerMartini/9814e6944485d2b90dda to your computer and use it in GitHub Desktop.
Python: Recursively goes through list of dictionaries and prints it's parent - children
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
tree = [ | |
{ | |
"parent" : "Chris", | |
"children" : [ | |
{ | |
"parent" : "Doug", | |
"children" : [] | |
}, | |
{ | |
"parent" : "Michelle", | |
"children" : [ | |
{ | |
"parent" : "Susan", | |
"children" : [] | |
} | |
] | |
} | |
] | |
}, | |
{ | |
"parent" : "Laura", | |
"children" : [ | |
{ | |
"parent" : "Michael", | |
"children" : [] | |
} | |
] | |
} | |
] | |
def add_menu_item(branch): | |
parent = branch["parent"] | |
print parent | |
for c in branch["children"]: | |
add_menu_item(c) | |
def build_sub_menu(tree): | |
for branch in tree: | |
add_menu_item(branch) | |
build_sub_menu(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment