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>When you write</p>

<pre><code>for item in alist:
</code></pre>

<p>You are actually creating a copy of each item in <code>liast</code> in the <code>item</code> variable, and you are <em>not</em> getting a reference to <code>item</code>.</p>

<p>However, <code>append</code> changes the list in-place and doesn't return a value, and that's why you're getting the values changed to <code>None</code> (due to the assignment - if you remove it you'll get the appending working fine).</p>

python