Niek de Klein
Niek de Klein

Reputation: 8834

How can I return values in a recursive function without stopping the recursion?

I have a structure with an x amount of lists in lists, and each list an x amount of tuples. I don't know beforehand how many nested lists there are, or how many tuples in each list.

I want to make dictionaries out of all the tuples and because I don't know the depth of the lists I want to use recursion. What I did was

def tupleToDict(listOfList, dictList):
    itemDict = getItems(list)  # a function that makes a dictionary out of all the tuples in list
    dictList.append(itemDict)
    for nestedList in listOfList:
         getAllNestedItems(nestedList, dictList)

    return dictList

this works, but I end up with a huge list at the end. I would rather return the itemDict at every round of recursion. However, I don't know how to (if it is possible) return a value without stopping the recursion.

Upvotes: 3

Views: 2294

Answers (4)

Don Question
Don Question

Reputation: 11614

You got two possible solutions:

  1. The generator approach: a function with a yield statement, which may be a hassle to implement in a recursive function. (Look at phihags proposal for an example)

  2. The callback approach: You call a helper-function/method from inside the recursion and can monitor the progress through a second outer function.

Here a non-recursive recursion example: ;-)

def callback(data): print "from the depths of recursion: {0}".format(data)

def recursion(arg, callbackfunc):
    arg += 1
    callbackfunc(arg)
    if arg <10:
        recursion(arg, callbackfunc)
    return arg

print recursion(1, callback)

Upvotes: 1

steve
steve

Reputation: 76

I'm not sure what you're trying to do, but you could try to make a recursive generator by using the yield statement to return the dict at the desired intervals. Either that or shove copies of it into a global list?

Upvotes: 1

phihag
phihag

Reputation: 288090

You're looking for yield:

def tupleToDict(listOfList):
    yield getItems(listofList)
    for nestedList in listOfList:
        for el in getAllNestedItems(nestedList):
            yield el

In Python 3.3+, you can replace the last two lines with a yield from.

You may want to rewrite your function to be iterative:

def tupleToDict(listOfList):
    q = [listOfList]
    while q:
        l = q.pop()
        yield getItems(l)
        for nestedList in listOfList:
            q += getAllNestedItems(nestedList)

Upvotes: 6

John3136
John3136

Reputation: 29266

Who are you going to return it to? I mean if your thread is busy running the recursive algorithm, who gets the "interim results" to process?

Best bet is to tweak your algorithm to include some processing before it recurses again.

Upvotes: 1

Related Questions