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>It's pointer variable in python.</p>

<p>The first example:</p>

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

<p>each item is pointing to each _item in alist but suddenly you change the item value, not the of the value of the _item in alist, as a result nothing change to alist</p>

<p>The second example: </p>

<pre><code>for item in alist:
    item = item.append(10)
</code></pre>

<p>each item is pointing to each _item in alist and then you append something to the item sharing the same memory location of _item, as a result value of _item in alist are changed and alist is changed too.</p>

python