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>Every turn through the loop you are adding to the list. You want to be further nesting the list, not adding more stuff onto it. You could do it something like this:</p>

<pre><code>def nest(x, n):
    for _ in range(n):
        x = [x]
    return x
</code></pre>

<p>Each turn through the loop, <code>x</code> has another list wrapped around it.</p>

python list python-2.7 nested