Is there a way i can store this variable without it getting reset
<p>I have this recursive function</p>

<pre><code>def recursive_search(x):
    y = []
    for i in x:
        if (i == tuple(i)) or (i == list(i)) or i == set(i):
            recursive_search(i)
        else:
            y.append(i)
    print(y)
print(recursive_search(("re",("cur",("sion",(" ",("foo",["bar",{"baz"}])))))))
</code></pre>

<p>which prints out</p>

<pre><code>['baz']
['bar']
['foo']
[' ']
['sion']
['cur']
['re']
None
</code></pre>

<p>when i set "print(y)" to "return y" it only prints out "['re']". if using global y is unsafe then what is another way i could do this.</p>

<p>Declare the second argument, which will be every time that the recursive function is called:</p>

<pre><code>def recursive_search(x,y=None):
    if y is None:
        y = []
    for i in x:
        if (i == tuple(i)) or (i == list(i)) or i == set(i):
            recursive_search(i, y)
        else:
            y +=[i]
    return y[:]

y = recursive_search(("re",("cur",("sion",(" ",("foo",["bar",{"baz"}]))))))
print(y)
z = recursive_search(("a",("brown",("fox",("jumps ",("over",["foo",{"bar"}]))))))
print(z)

['re', 'cur', 'sion', ' ', 'foo', 'bar', 'baz']
['a', 'brown', 'fox', 'jumps ', 'over', 'foo', 'bar']
</code></pre>

python python-3.x recursion