Nesting a string inside a list n times ie list of a list of a list
<pre><code>def nest(x, n):
    a = []
    for i in range(n):
        a.append([x])
    return a

print nest("hello", 5)
</code></pre>

<p>This gives an output</p>

<pre><code>[['hello'], ['hello'], ['hello'], ['hello'], ['hello']]
</code></pre>

<p>The desired output is </p>

<pre><code>[[[[["hello"]]]]]
</code></pre>

<p>Here is a pythonic recursion approach:</p>

<pre><code>In [8]: def nest(x, n):
   ...:     return nest([x], n-1) if n else x 
</code></pre>

<p>DEMO:</p>

<pre><code>In [9]: nest(3, 4)
Out[9]: [[[[3]]]]

In [11]: nest("Stackoverflow", 7)
Out[11]: [[[[[[['Stackoverflow']]]]]]]
</code></pre>

python list python-2.7 nested