Python: When do two variables point at the same object in memory?
<p>Here is an example:</p>

<pre><code>l = [1, 5, 9, 3]
h = l

h[0], h[2] = h[2], h[0]

print(h) # [9, 5, 1, 3]
print(l) # [9, 5, 1, 3]

h = h*2
print(h) # [9, 5, 1, 3, 9, 5, 1, 3]
print(l) # [9, 5, 1, 3]
</code></pre>

<p>My understanding was that calling setting <code>h = l</code> would simply point <code>h</code> at the same item in memory that <code>l</code> was pointing at. So why is it that in the last 3 lines, <code>h</code> and <code>l</code> don't give the same results?</p>

<p>The assignment does make <strong>h</strong> point to the same item as <strong>l</strong>.  However, it does not permanently weld the two.  When you change <strong>h</strong> with <strong>h = h * 2</strong>, you tell Python to build a doubled version elsewhere in memory, and then make <strong>h</strong> point to the doubled version.  You haven't given any instructions to change <strong>l</strong>; that still points to the original item.</p>

python pointers