Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Created December 11, 2015 13:13
Show Gist options
  • Save JokerMartini/9814e6944485d2b90dda to your computer and use it in GitHub Desktop.
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
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