Skip to content
Speed Test

Speed Test

Python list.insert(0) vs deque: Why Adding to the Front of a List Is Slow

· Python list.insert(0) vs deque | deque hazaar guna se zyada fast

Adding to the front of a Python list shifts every other item, so list.insert(0) is O(n) per insert. collections.deque adds to either end in O(1). On my Mac, 100,000 front inserts took about 2.5 s with a list and about 2 ms with deque.

Interview SheetPractise the patterns behind interview problemsProblems grouped by pattern, each with the idea first and the code in four languages.