List modification in a loop
<p><em>I'm a newcomer to Python but I understand that things should not be done this way, so please consider the following code snippets as purely educational :-)</em></p>

<p>I'm currently reading 'Learning Python' and trying to fully understand the following example:</p>

<pre><code>&gt;&gt;&gt; L = [1, 2, 3, 4, 5]
&gt;&gt;&gt; for x in L:
...    x += 1
...
&gt;&gt;&gt; L
[1, 2, 3, 4, 5]
</code></pre>

<p>I did not understand if this behavior was somewhat related to the immutability of the numeric types, so I've run the following test:</p>

<pre><code>&gt;&gt;&gt; L = [[1], [2], [3], [4], [5]]
&gt;&gt;&gt; for x in L:
...    x += ['_']
...
&gt;&gt;&gt; L
[[1, '_'], [2, '_'], [3, '_'], [4, '_'], [5, '_']]
</code></pre>

<p><strong>Question: what makes the list unchanged in the first code and changed in the second ?</strong></p>

<p>My intuition is that the syntax is misleading and that:</p>

<ul>
<li><code>x += 1</code> for an integer really means <code>x = x + 1</code> (thus assigning a new reference)</li>
<li><code>x += ['_']</code> for a list really means <code>x.extend('_')</code> (thus changing the list in place)</li>
</ul>
