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>The <code>=</code> operator does not make any change in the second code, using <code>.append</code> causes changes in <code>alist</code>. Use following line as the third line in the second code. You will see the same result:</p>

<pre><code>item.append(10)
</code></pre>

<p>In the first code <code>item</code> point to another object by <code>item=1</code>, so <code>alist</code> does not change. In the second code you make change on <code>alist</code> by calling <code>append</code> method of it.</p>

python