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>In the first example <code>item</code> is bound to each element in list <code>alist</code>, and then <code>item</code> is <em>rebound</em> to the integer <code>1</code>. This does not change the element of the list - it merely <em>rebinds</em> the name <code>item</code> to the <code>int</code> object <code>1</code>.</p>

<p>In the second example the list element (itself a list) is mutated by <code>append()</code>. <code>item</code> is still bound to the sub-list so <code>item.append()</code> mutates the sub-list.</p>

python