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>You need to capture the return value and using that to construct your answer:</p>

<pre><code>def recursive_search(x):
    y = []
    for i in x:
        if type(i) in (tuple, list, set):
            y.append(recursive_search(i))
        else:
            y.append(i)
    return y

print(recursive_search(("re",("cur",("sion",(" ",("foo",["bar",{"baz"}])))))))
# ['re', ['cur', ['sion', [' ', ['foo', ['bar', ['baz']]]]]]]
</code></pre>

<p>If you need something other than nesting you can do something else with the return.</p>

python python-3.x recursion