Different behaviour when operating on equivalent multidimensional lists
<p>When I operate on two, I think equivalent multidimensional lists, I have different outcomes. The only difference between the lists is how they are created. I'm using Python 3.4.3</p>

<pre><code>&gt;&gt;&gt; b = [[1,2],[1,2]]
&gt;&gt;&gt; b[0][0] += 1
&gt;&gt;&gt; b
[[2, 2], [1, 2]]
&gt;&gt;&gt; b = [[1,2]] * 2
&gt;&gt;&gt; b
[[1, 2], [1, 2]]
&gt;&gt;&gt; b[0][0] += 1
&gt;&gt;&gt; b
[[2, 2], [2, 2]]
</code></pre>

<p>As you can see, both b's and the operations on them are the same, but the outcome is not. I'm guessing that it has something to do with the way they are created since that is the only difference, but I don't see how.</p>

<p>Its the same with Python 2.7.6</p>

<pre><code>&gt;&gt;&gt; b = [[1,2],[1,2]]
&gt;&gt;&gt; b
[[1, 2], [1, 2]]
&gt;&gt;&gt; c = [[1,2]] * 2
&gt;&gt;&gt; c
[[1, 2], [1, 2]]
&gt;&gt;&gt; c == b
True
&gt;&gt;&gt; b[0][0] += 1
&gt;&gt;&gt; b
[[2, 2], [1, 2]]
&gt;&gt;&gt; c[0][0] += 1
&gt;&gt;&gt; c
[[2, 2], [2, 2]]
&gt;&gt;&gt; c == b
False
&gt;&gt;&gt; 
</code></pre>

<p>In the second case you're making what's known as a shallow copy of the <code>[1,2]</code> list. Essentially what that means is that somewhere in memory you have the list <code>[1,2]</code>, and when you write <code>[[1,2]]*2</code> you're saying you want two references to that same list. Thus, when you change one of the lists, you're actually changing the list that both items in <code>b</code> are referring to.</p>

python list