We know that objects of type list
, because they are based on dynamic vectors, have a maximum internal capacity. We also know that when full capacity is reached and we want to insert a new value into the list, Python allocates a new array with a larger capacity than the previous one and transfers all values from the array to the new array . But how does the language allocate a new portion of memory without losing the reference of the previous one?
lst = []
print(id(lst))
for i in range(999):
lst.append(i)
print(id(lst))
When executing the code, we can see that the id's will be the same, even after the resizing operations. How is this possible?