When removing an element from the front of a list in python by doing list.pop(0)
. Will this create a new array or just move the pointer to the new first element. And if not what are the reasons for not doing so?
Asked
Active
Viewed 100 times
-1

PKuhn
- 1,338
- 1
- 14
- 30
-
2All the remaining elements will be shifted. If you are popping from the front use a `deque` – Padraic Cunningham Jun 12 '16 at 21:42
-
http://stackoverflow.com/questions/195625/what-is-the-time-complexity-of-popping-elements-from-list-in-python – user2864740 Jun 12 '16 at 22:10
1 Answers
4
Will this create a new array or just move the pointer to the new first element.
Neither. It shifts all remaining list elements to fill the gap. While it could be implemented by changing the pointer to the first element, doing so would introduce additional time and space overhead for other operations. If you need efficient insertion and removal at both ends of a sequence, collections.deque
does that job.

user2357112
- 260,549
- 28
- 431
- 505