python: loop a list of list and assign value inside the loop
<p>I have the following code, why the first one doesn't change <code>alist</code> while the second changes it?</p>

<pre><code>alist = [[1,2], [3,4], [5,6]]
for item in alist:
    item = 1
print(alist)

alist = [[1,2], [3,4], [5,6]]
for item in alist:
    item = item.append(10)
print(alist)
</code></pre>

<p>Are you forgetting that <code>list.append()</code> does not return the list itself but actually modifies the list in place?</p>

<p><code>item = 1</code> does as expected. For the rest of the for-loop, item is now <code>1</code>, and not the list it originally was. It won't reassign what <code>item</code> is, that's not what for-loops do.</p>

<p>However, in your second loop, you're now assigning <code>item = None</code>, because the append function does not return anything but it appends the item to the list in place:</p>

<pre><code>&gt;&gt;&gt; L = [1, 2, 3]
&gt;&gt;&gt; L.append(4)
&gt;&gt;&gt; L
[1, 2, 3, 4]
</code></pre>

<p>Thus, your code is basically saying "go through each sublist in my main list and append <code>10</code> to it".</p>

python