I'd like to find the parent sibling in this array.
arr = {
'children': [
{
'id':'item_1',
'title':'Item 1',
'children': [
{
'id':'item_a',
'title':'Item A'
},
{
'id':'item_b',
'title':'Item B'
},
{
'id':'item_c',
'title':'Item C',
'children': [
{
'id':'item_i',
'title':'Item I'
},
{
'id':'item_ii',
'title':'Item II'
}
]
}
]
}
]
}
For instance if I put "item_b" into the method it must return "item_1" (ignoring "children" as a parent). Something similar to this:
>>> parent = get_parent(arr, 'item_i', ['children'])
>>> parent['id']
item_c
>>> parent = get_parent(arr, 'item_1', ['children'])
>>> parent['id']
None
My id's are unique, so I know there will only ever be one parent.
I've looked at examples by mgilson and Claudiu, but I can't get my head around how to change these to what I'm after. Claudius solution looks like the right direction, but with my array it just returns []
.
Edit: ewcz solution works great. I modified it a bit to allow getting the title as well:
def get_parent(arr, item_id):
L = [(None, arr)]
while L:
parent_item, item = L.pop()
curr_id = item.get('id', None)
if curr_id == item_id:
return parent_item
children = item.get('children', [])
for child in children:
L.append((item, child))
parent = get_parent(arr, 'item_i')
print(parent.get('id'))
print(parent.get('title'))