Python - recursion not working
<p>I have a following code:</p>

<pre><code>d = {'init':
       [{'solve':
                 [{'subsolve':
                       [{'vals': [{'Blade summary': 'asdf'},
                                  {'Blade summary': 'fdsa'}]}]},
                  {'subsolve':
                       [{'vals': [{'Blade summary': 'ffff'}]}]}]},
        {'solve':
                 [{'subsolve':
                       [{'vals': 'bbbb'}]}]}]}

def parseDics(lst, mainReg):
    print('call')
    for dic in lst:
        for key, vals in dic.items():
            if key == mainReg:
                if mainReg == 'vals':
                    yield vals
                parseDics(vals, 'vals')
            else:
                parseDics(vals, mainReg)



if __name__=='__main__':
    pp.pprint(list(parseDics(d['init'], 'solve')))
</code></pre>

<p>The function itself is not complete, but that's not a problem for now. The problem is, it appears, that recursive calls don't work.</p>

<p>If I try to run it now, I'll get only this output:</p>

<pre><code>call
[]
</code></pre>

<p>So the function was called only once. When I try to step into the nested function call (I'm using PyCharm), then I'm simply not not able to and the function call is "over-stepped".</p>

<p>What am I doing wrong? Why isn't my function called recursively?</p>

<p><code>parseDics</code> isn't a regular function, it's a generator. So you need to call it like a generator, rather than a regular function, otherwise it won't work. The first call works because when you call <code>list(parseDicts(...))</code>, the list constructor is calling <code>parseDicts</code> as a generator. But within <code>parseDicts</code>, you try to recursively call <code>parseDicts</code> as a function, which doesn't work.</p>

<p>Change the recursive calls to use <code>yield from</code> if you are using Python 3.3+:</p>

<pre><code>def parseDics(lst, mainReg):
    print('call')
    for dic in lst:
        for key, vals in dic.items():
            if key == mainReg:
                if mainReg == 'vals':
                    yield vals
                yield from parseDics(vals, 'vals')
            else:
                yield from parseDics(vals, mainReg)
</code></pre>

<p>For older versions of Python, you would need to iterate over those recursive calls and yield each value that generate:</p>

<pre><code>def parseDics(lst, mainReg):
    print('call')
    for dic in lst:
        for key, vals in dic.items():
            if key == mainReg:
                if mainReg == 'vals':
                    yield vals
                for val in parseDics(vals, 'vals'):
                    yield val
            else:
                for val in parseDics(vals, mainReg):
                    yield val
</code></pre>

<p>Calling a generator as a function merely creates the generator, it doesn't run it. E.g.:</p>

<pre><code>&gt;&gt;&gt; def my_gen():
    print("my_gen()")
    for i in range(5):
        print(i)
        yield i

&gt;&gt;&gt; my_gen()
&lt;generator object my_gen at 0x00000000045B6B48&gt;

&gt;&gt;&gt; list(my_gen())
my_gen()
0
1
2
3
4
[0, 1, 2, 3, 4]
</code></pre>

python python-3.x dictionary recursion pycharm