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>instead of appending you sould wrap <code>x</code> and call recursively the method till call number is lesser than <code>n</code></p>

<pre><code>def nest(x, n):
    if n &lt;= 0:
        return x
    else:
        return [nest(x, n-1)]
</code></pre>

python list python-2.7 nested