0

I have recursive function which walk nested dict and return needed key's value:

def recurs(l):
    for key in l.keys():
        if key == '_events':
            return l[key]
        else:
            recurs(l[key])

c=recurs(d)
print c

And how i can get these values?

2
  • Right now its return None, but dict have two lists, and then i try to use yield its return empty iter object Commented Aug 1, 2011 at 12:41
  • 1
    It does not make sense (logically) to return a (one) value from the function as it calls recurs potentially several times. So the question is, what kind of return value do you expect? A single value? A list? Commented Aug 1, 2011 at 12:44

2 Answers 2

3

The easiest way to get a flattened iterator is to write a generator function:

def recurs(l):
    for key in l.keys():
        if key == '_events':
            yield l[key]
        else:
            for x in recurs(l[key]):
                yield x
Sign up to request clarification or add additional context in comments.

Comments

1

Just return it:

def recurs(l):
    for key in l.keys():
        if key == '_events':
            return l[key]
        else:
            return recurs(l[key])

4 Comments

That will terminate the loop after the first iteration. Although I don't understand what the loop is for...
and instead of two lists of values ​​I get one
@Felix I suppose OP need single value.
@Roman: But which one? Why do you think he wants the first?

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.