Will del a [0] in Python copy the entire list?

I am trying to use a simple s list del a[0]to simulate deque.popleft(). I just want to understand how it works delin Python. For instance:

a = [0,1,2,3,4,5]

ais in continuous space in memory. After I call del a[0], Python will allocate a new space and copy there 1,2,3,4,5, or it will just give a anew address (which matches a = a[1:]).

If it allocates a new space, does this mean that it del a[0]is an operation _O (len (a) _)?

If del a[0]matches a = a[1:], will Python free up memory space removed from the array?

+4
source share
1 answer

See this answer: How is the Python list implemented?

Python lists are essentially arrays of pointers. Therefore, when removing from the front moves all the elements in the entire list to a new position within the same memory space, the whole list is much smaller than you would expect if each element were actually stored in the list, and therefore shuffling is much faster. in terms of fixed time costs.

But, of course, del a[0]this is an operation O (len (a)), therefore it dequeexists.

, , . / , , .

:

: , , , , .

+5

Source: https://habr.com/ru/post/1570424/


All Articles