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>This is a somewhat unintuitive behavor of variables. It happens because, in Python, variables are always references to values.</p>

<h1>Boxes and tags</h1>

<p>In some languages, we tend to think about variables as "boxes" where we put values; in Python, however, they are references, and behave more like tags or "nicknames" to values. So, when you attribute 1 to <code>item</code>, you are changing only the variable reference, not the list it is pointing to.</p>

<p>A graphic representation can help. The image below represents the list created by <code>alist = [[1,2], [3,4], [5,6]]</code></p>

<p><a href="http://i.stack.imgur.com/KpcjH.png"><img src="http://i.stack.imgur.com/KpcjH.png" alt="A list with three sublists"></a></p>

<p>Given that, let's see what happens when we execute your first loop.</p>

<h1>The first loop</h1>

<p>When you execute <code>for item in alist</code>, you are asking the interpreter to take each value from the list, one per time, put it on the variable <code>item</code> and do some operation in it. In the first operation, for example, we have this new schema:</p>

<p><a href="http://i.stack.imgur.com/wPXKX.png"><img src="http://i.stack.imgur.com/wPXKX.png" alt="Now a variable points to a sublist"></a></p>

<p>Note that we do not copy the sublist to <code>item</code>; instead, we <em>point</em> to it through <code>item</code>. Then, we execute <code>item = 1</code> â but what does it mean? It mean that we are making <code>item</code> point to the value <code>1</code>, instead of pointing to the sublist:</p>

<p><a href="http://i.stack.imgur.com/snmRy.png"><img src="http://i.stack.imgur.com/snmRy.png" alt="item points to other value now"></a></p>

<p>Note that the old reference is lost (it is the red arrow) and now we have a new. But we just changed a variable pointing to a list â we did not alter the list itself.</p>

<p>Then, we enter to the second iteration of the loop, and now <code>item</code> points to the second sublist:</p>

<p><a href="http://i.stack.imgur.com/ERT2X.png"><img src="http://i.stack.imgur.com/ERT2X.png" alt="item pointing to the second sublist"></a></p>

<p>When we execute <code>item = 1</code>, again, we just make the variable point to aonther value, without changing the list:</p>

<p><a href="http://i.stack.imgur.com/7jxHt.png"><img src="http://i.stack.imgur.com/7jxHt.png" alt="enter image description here"></a></p>

<p>Now, what happens when we execute the second loop?</p>

<h1>The second loop</h1>

<p>The second loop starts as the first one: we make <code>item</code> refer to the first sublist:</p>

<p><a href="http://i.stack.imgur.com/wPXKX.png"><img src="http://i.stack.imgur.com/wPXKX.png" alt="Now a variable points to a sublist"></a></p>

<p>The first difference, however, is that we call <code>item.append()</code>. <code>append()</code> is a <em>method</em>, so it can change the value of the object it is calling. As we use to say, we are sending a <em>message</em> to the object pointed by <code>item</code> to append the value 10. In this case, the operation is <em>not</em> being made in the variable <code>item</code>, but directly in the object it refers! So here is the result of calling <code>item.append()</code>:</p>

<p><a href="http://i.stack.imgur.com/XXcfr.png"><img src="http://i.stack.imgur.com/XXcfr.png" alt="A list has grown"></a></p>

<p>However, we do not only append a value to the list! We also assign the value returned by <code>item.append()</code>. This will sever the <code>item</code> reference to the sublist, but here is a catch: <code>append()</code> returns <code>None</code>.</p>

<p><a href="http://i.stack.imgur.com/7T77S.png"><img src="http://i.stack.imgur.com/7T77S.png" alt="enter image description here"></a></p>

<h1>The <code>None</code> value</h1>

<p><code>None</code> is a value that represents, basically, the unavailability of a relevant value. When a function returns <code>None</code>, it is saying, most of the time, "I have nothing relevant to give you back." <code>append()</code> does change its list directly, so there is nothing it needs to return.</p>

<p>It is important because you probably believed <code>item</code> would point to the appended list <code>[1, 2, 10]</code>, right? No, now it points to <code>None</code>. So, you would expect the code below...</p>

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

<p>To print something like this:</p>

<pre><code>[1, 2, 10]
[3, 4, 10]
[5, 6, 10]
</code></pre>

<p>But this <em>does not</em> happen. <em>This</em> is what happens:</p>

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

<p>Yet, as we commented, the <code>append()</code> method changed the lists themselves. So, while the <code>item</code> variable was useless after the assignment, the final list is modified!</p>

<pre><code>&gt;&gt;&gt; print alist
[[1, 2, 10], [3, 4, 10], [5, 6, 10]]
</code></pre>

<p>If you want to use the appended list inside the loop, just do not assign the method returned value to <code>item</code>. Do this:</p>

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

<p>This works because <code>item</code> will still point to the list.</p>

<h1>Conclusion</h1>

<p>References are somewhat complex to understand at first, let alone master. Yet, they are really powerful and can be learned if you follow examples etc. Your example is a bit complicated because there is more happening here.</p>

<p>The <a href="http://www.pythontutor.com/">Python Tutor</a> can help you understand what is going on, because it executes each step graphically. <a href="http://www.pythontutor.com/visualize.html#code=alist%20%3D%20%5B%5B1,2%5D,%20%5B3,4%5D,%20%5B5,6%5D%5D%0Afor%20item%20in%20alist%3A%0A%20%20%20%20item%20%3D%201%0Aprint(alist%29%0A%0Aalist%20%3D%20%5B%5B1,2%5D,%20%5B3,4%5D,%20%5B5,6%5D%5D%0Afor%20item%20in%20alist%3A%0A%20%20%20%20item%20%3D%20item.append(10%29%0Aprint(alist%29&amp;cumulative=false&amp;curInstr=0&amp;heapPrimitives=false&amp;mode=display&amp;origin=opt-frontend.js&amp;py=2&amp;rawInputLstJSON=%5B%5D&amp;textReferences=false">Check your own code running there!</a></p>

python