how to get the declaration of a list done during the unnesting recursively?
<p>I have been getting my head around to do the recursion <strong>without using any global variable</strong>. If am passing the lists the lists of lists ... and resultantly, i should have all the elements from the input in a single list. The problem is every time i call the function the list is declared from the start. </p>

<pre><code>    def unnest (alist):  
        List_ = list()

        for elements in alist: 
            if type(elements) == list: 
                unnest(elements)
            else: 
                List_.append(elements)
        return List_
    if __name__=='__main__':
        unnest([1,2,[3,4,5,[6,7,[8,9,[10,11,[12,[13,[14]]]]]]]])
</code></pre>

<p>Try this, There are inbuilt module support for making flattern.</p>

<pre><code>In [47]: from compiler.ast import flatten
In [48]: lst = [1,2,[3,4,5,[6,7,[8,9,[10,11,[12,[13,[14]]]]]]]]
In [49]: flatten(lst)
Out[49]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
</code></pre>

<p>Recursion method,</p>

<pre><code>def flatern(l,ml):
    for i in l:
        if isinstance(i,list):
            flatern(i,ml)
        else:
            ml.append(i)
    return ml
</code></pre>

<p><strong>Result</strong></p>

<pre><code>In [52]: flatern(lst,[])
Out[52]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
</code></pre>

python python-3.x recursion