-1

I wanted to understand how memory allocation works in python lists.

Refer this image

When I define a list as above, I can see that for some elements, the addresses are exactly the same eg. 0th and 2nd element.

On the other hand when all elements are distinct, I can see that all element's addresses are distinct. Does this mean that the list internally maintains some kind of a map which prevents creating copies of elements which are repeating in the list?

Refer this image

Thanks!

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 27 '22 at 14:14

1 Answers1

0

Take a look on this post for internal list information.

I'm not sure to got your point about copies. If I understand correctly, the answer is: No. List are no doing this kind of optimization. But, because lists keep references, there are not doing any copy.

You probably have been misled by "plain integer object", more information on this post and of course on the Python doc that is saying:

The current implementation keeps an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get back a reference to the existing object.