3

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.

1
  • 2
    If you have a list of tuples, you can create a dictionary by simply using dict(l) on it ... Commented Mar 15, 2012 at 11:13

4 Answers 4

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome use of yield! Thanks!
There may be use-cases where a generator-pattern won't be sufficient, because a lack of iterable objects. Approximation- , boundary-conditions or some fractal-algorithm comes to mind. Generators create iterables on the fly, but if you don't need an iterative, then you may have to look for another approach or refactor your original solution to match the generator-pattern. - Just a site-note to keep in mind! ;-)
1

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.

Comments

1

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?

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.