How can I rewrite this list comprehension as a for loop?
<p>How can I rewrite this using nested for loops instead of a list comprehension? </p>

<pre><code>final= [[]]  
    for i in array_list:
        final.extend([sublist + [i] for sublist in final])


    return final
</code></pre>

<p>This code also seems to give the same results as your code. </p>

<pre><code>final = [[]]
for i in array_list:
    for sublist in list(final):
        final.extend([sublist + [i]])
return final
</code></pre>

<p>It seems like your code takes the elements of the last iteration and combines them with the element of the current iteration (See 'Example' below). In order to do this with a traditional for loop, you need to prevent the list that is being looped over from being updated while looping. I do this by breaking the link with the 'final' list variable. This can be done with the list() function or by slicing it (then you would need to replace  with  as proposed by Morgan).</p>

<p><strong>Example</strong></p>

<p>For the array_list of [1,2,3] you get the following. Each line (except for the lines) is a new element of the 'final' list variable.</p>

<pre><code>[]
--------------------------------
1           &gt; array_list[0]
--------------------------------
2           &gt; array_list[1]
1, 2        &gt; array_list[0] + array_list[1]
--------------------------------
3           &gt; array_list[2]
1, 3        &gt; array_list[0] + array_list[2]
2, 3        &gt; array_list[1] + array_list[2]
1, 2, 3     &gt; array_list[0] + array_list[1] + array_list[2]
</code></pre>

python