Skip to content
Advertisement

Why the location of python list is not being changed if the size is increased?

As far as I know, python list is a dynamic array. So when we reach a certain size, the capacity of that list will be increased automatically. But the problem is, unlike dynamic array of c or c++, even after increasing the capacity of list instance, the location is not being changed. Why is it happening?

I’ve tested this using the following code block

l = []

print(l.__sizeof__())
print(id(l))

for i in range(5_000_000):
    l.append(i)

print(l.__sizeof__())
print(id(l))

Advertisement

Answer

In CPython (the implementation written in C distributed by python.org), a Python object never moves in memory. In the case of a list object, two pieces of memory are actually allocated: a basic header struct common to all variable-size Python container objects (containing things like the reference count, a pointer to the type object, and the number of contained objects), and a distinct block of memory for a C-level vector holding pointers to the contained Python objects. The header struct points to that vector.

That vector can change size in arbitrary ways, and the header struct will change to point to its current location, but the header struct never moves. id() returns the address of that header struct. Python does not expose the address of the vector of objects.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement