First of all, I\'m still a newb to python so please take it easy on me.
I\'ve done my research and I have a basic understanding of how to write a recursive function
I would approach this as follows:
def recursive(input, output=None):
if output is None:
output = {} # container to store results
if 'children' in input:
# do whatever, add things to output
recursive(input['children'], output)
return output
This way, the output
dictionary is accessible to all depths of iteration and gets return
ed with all contents at the end. This means that you don't have to explicitly deal with the return
values of the recursive calls.
Depending on exactly what you have, it may look more like:
def recursive(input, output=None):
if output is None:
output = {} # container to store results
if 'children' in input:
for child in input['children']:
# do whatever, add things to output
recursive(child, output)
return output
And output
may be a different container (e.g. list
, set
).