Why Python's list.pop(0) slows down and when to use deque instead
Calling list.pop(0) in Python works well for small queues but becomes slow as they grow, because removing the first item forces every remaining item to shift one position. A deque, short for double-ended queue, allows adding or removing items at either end in constant time. In a test on Python 3.10 with 50,000 items, emptying a list with pop(0) took about 0.189 seconds, while using deque.popleft() took about 0.002 seconds. The article recommends deque for queues, breadth-first search and sliding windows, while lists remain suitable for stacks and index-based access in the middle. Exact timings will vary by machine, but the performance gap widens as queues get larger.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in