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>This is very-well understood behavior in Python.</p>

<pre><code>a = [[], []] # two separate references to two separate lists
b = [] * 2 # two references to the same list object
a[0].append(1) # does not affect a[1]
b[0].append(1) # affects b[0] and b[1]
</code></pre>

<p><a class='doc-link' href="http://stackoverflow.com/documentation/python/3553/common-pitfalls/12259/list-multiplication-and-common-references#t=201610041521133519857">See this documentation</a></p>

python list