Memory allocation in python
<p>I create a list with three elements named l after that I copy all the content of the list into the list y. But when I print the address of them in the memory I don't understand why this is not the same address. Why y is not a reference of l, and if I want that y will be a reference of l so that they will have the same address. How can I do this ?</p>

<p>This is my code</p>

<pre><code>l = [8,12,3]
y = l[:]

print l
print y

print id(l)
print id(y)
</code></pre>

<p>Display on the screeen :</p>

<pre><code>[8, 12, 3]
[8, 12, 3]
40894592 
40837072
</code></pre>

<p>A good point is that if you write this statement</p>

<pre><code>l = [8,12,3],
</code></pre>

<p>Python creates a pointer from the object <code>l</code> to a list <code>[8, 12, 3]</code>. </p>

<p>In case you make this statement</p>

<pre><code>y = l,
</code></pre>

<p>Then the object <code>y</code> points to <code>l</code> and if you change <code>l</code>, <code>y</code> will be also changed. </p>

<p>In case you write</p>

<pre><code>y = list(l),
</code></pre>

<p>the object <code>y</code> points to a new list. Then if you modify <code>l</code>, values in <code>y</code> remain unchanged.</p>

python memory