A Single Line Made a Python Queue 2,500 Times Slower: Why list.pop(0) Is Costly
A developer found that processing one million tasks from a Python list took about 58 seconds because each pop(0) call shifted every remaining element. Replacing the list with collections.deque and using popleft() cut the runtime to roughly 0.023 seconds on Python 3.15.2 on an Apple M4. The article explains that list removal and insertion at the front cost O(n), so total work grows quadratically as data increases. It also covers related slowdowns, such as repeatedly building lists with out = out + [x] and string concatenation with += when the string has extra references. The author recommends deque for queues, append or += for lists, and str.join for building strings.
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