I have a dictionary like this:
{
1: [6,8,10],
2: [3,4],
5: [11],
6: [9,13],
13: [14],
}
I would like to traverse each key-value pair and for each key find all children and create this (e.g. for key=1) [6,8,10,9,13,14] (because 6->[9,13] and 13->[14]
i tried this recursion code but failed:
def get_all_children(key):
try:
arr = _dict[key]
except KeyError:
return []
for item in arr:
return arr + get_all_children(item)