python shallow copies can they be similar to deepcopies
<p>I am playing about with shallow copies in python. I came across a gotcha that I was not expecting.</p>

<p>My assumption was that a shallow copy was a new instance of a class with references to the objects in the class. This behavior is shown below.</p>

<pre><code>&gt;&gt;&gt; a = { 'a': 1, 'b':2, 'c': [[1,2],2,3,4,5,6] }
&gt;&gt;&gt; c = copy.copy(a['c'])
&gt;&gt;&gt; d = a['c']
&gt;&gt;&gt; a['c'] is c
False
&gt;&gt;&gt; a['c'][0] is c[0]
True
&gt;&gt;&gt; a['c'] is d
True
&gt;&gt;&gt; a['c'][0] is d[0]
True
</code></pre>

<p>What surprised me was the following. As the elements of the shallow copied list are references to the elements in the list from <code>a</code> I assumed when I changed the mutable 1st element it would also change in <code>a</code>.</p>

<pre><code>&gt;&gt;&gt; c[0] = [3,3]
&gt;&gt;&gt; c
[[3, 3], 2, 3, 4, 5, 6]
&gt;&gt;&gt; a
{'a': 1, 'c': [[1, 2], 2, 3, 4, 5, 6], 'b': 2}
&gt;&gt;&gt; a['c'][0] is c[0]
False
</code></pre>

<p>I see since the change the first element is no longer a reference of <code>a</code>.</p>

<h3>My Question:</h3>

<p>If I changed all elements in the list would it be similar to a deepcopy?</p>
