The article mentions the advantages of deque, but neglects the mention its disadvantages. While deque has fast pop/append from both ends, it has slow item access. Use deque only if you need insert/remove to be fast from both ends, and don't care about read speeds. List has constant time append/pop from one end and also constant time access anywhere in the list. You should almost always prefer list over deque, which is why list is builtin. Answer from sushibowl on reddit.com
🌐
Reddit
reddit.com › r/learnpython › use deque instead of list always ?
r/learnpython on Reddit: Use deque instead of list always ?
December 28, 2017 -

I recently came across the benefits of deque over list.

Should I be using deque instead of list always now ? Is there a scenario where I would need list instead ?

Thanks for help.

Top answer
1 of 6
15

The article mentions the advantages of deque, but neglects the mention its disadvantages. While deque has fast pop/append from both ends, it has slow item access.

Use deque only if you need insert/remove to be fast from both ends, and don't care about read speeds. List has constant time append/pop from one end and also constant time access anywhere in the list. You should almost always prefer list over deque, which is why list is builtin.

2 of 6
5

I wrote a bit of test code based largely on the OP's code and tested a few different operations:

  • del a[0]

  • del a[-1]

  • a.pop()

  • a.append()

  • a.insert(0) # not for python 2

The results show that using a list can be a lot slower than a deque in certain cases. In other cases the list operations are faster than the deque operations. The code is here and the results are here. The code also runs under python 2, and the results for that are here.

The results for python 3 show that "del a[0]" on a list is slower for large N, but for small N < 25 "del a[0]" is faster. Doing "del a[-1]" is always faster for the list when N is large (something greater than 100). And doing "a.insert(0)" on a list is always slower, especially for large N where lists slow down a lot:

Doing "a.insert(0)" on a list and deque using python3
-----------------------------------------------------------------
Size      10: list took   0.000024s, deque took 0.000007s
Size     100: list took   0.000045s, deque took 0.000036s
Size    1000: list took   0.000932s, deque took 0.000369s
Size   10000: list took   0.039175s, deque took 0.002529s
Size  100000: list took   3.606581s, deque took 0.024473s
-----------------------------------------------------------------

For me, the takeaway is: just get the algorithm right first using lists or deques as you wish. You can, of course, use one or the other if you know it's always better. Then, if performance is lacking, you must profile your code. Using a list or deque just because you think it's faster smells of premature optimization. Get the code working correctly first, then stress it with large amounts of data to see if it's fast enough.

🌐
GeeksforGeeks
geeksforgeeks.org › python › deque-vs-list-in-python
Deque vs List in Python - GeeksforGeeks
July 23, 2025 - Deque is a doubly linked list optimized for fast insertions and deletions at both ends. In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list.
Discussions

Should I use a Python deque or list as a stack? - Stack Overflow
I want a Python object that I can use as a stack. Is it better to use a deque or a list and does it make a difference whether I have a small number of elements or a large number of elements? More on stackoverflow.com
🌐 stackoverflow.com
deque over lists?
You left out a key detail in you're comparison of O(1) vs O(n), the docs say: Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction. The second paragraph explains the difference: Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation. Meaning that purely for adding a value to the beginning of the sequence, deque's are faster, as the name of 'double ended queue' already suggests. This does not mean they have any improvements for other aspects of list operations, which for example this SO answer explains: What this means is that Python lists are much better for random-access and fixed-length operations, including slicing, while deques are much more useful for pushing and popping things off the ends, with indexing (but not slicing, interestingly) being possible but slower than with lists. So only if you actually need the double endedness then a deque would come to mind. Another extra feature for which I use deque's myself is a fifo queue with a maximum length that's enforced by dropping items from the front of the queue. Of course that's a very specific use case that just happens to work for deque's, if it wasn't there then I would implement it myself anyway. Using its rotate() to make it a shift register is another example of this. More on reddit.com
🌐 r/learnpython
2
3
February 7, 2018
design - Are there any use cases for List when Deques and Arrays are available? - Software Engineering Stack Exchange
I've been thinking about this over the past few weeks, and I've come up with no good arguments. My perspective is from Java, but if anyone has any language-specific cases outside of this language,... More on softwareengineering.stackexchange.com
🌐 softwareengineering.stackexchange.com
February 2, 2018
Use deque instead of list always ?

The article mentions the advantages of deque, but neglects the mention its disadvantages. While deque has fast pop/append from both ends, it has slow item access.

Use deque only if you need insert/remove to be fast from both ends, and don't care about read speeds. List has constant time append/pop from one end and also constant time access anywhere in the list. You should almost always prefer list over deque, which is why list is builtin.

More on reddit.com
🌐 r/learnpython
8
13
December 28, 2017
Top answer
1 of 5
159

Could anyone explain me what I did wrong here

Yes, your timing is dominated by the time to create the list or deque. The time to do the pop is insignificant in comparison.

Instead you should isolate the thing you're trying to test (the pop speed) from the setup time:

In [1]: from collections import deque

In [2]: s = list(range(1000))

In [3]: d = deque(s)

In [4]: s_append, s_pop = s.append, s.pop

In [5]: d_append, d_pop = d.append, d.pop

In [6]: %timeit s_pop(); s_append(None)
10000000 loops, best of 3: 115 ns per loop

In [7]: %timeit d_pop(); d_append(None)
10000000 loops, best of 3: 70.5 ns per loop

That said, the real differences between deques and list in terms of performance are:

  • Deques have O(1) speed for appendleft() and popleft() while lists have O(n) performance for insert(0, value) and pop(0).

  • List append performance is hit and miss because it uses realloc() under the hood. As a result, it tends to have over-optimistic timings in simple code (because the realloc doesn't have to move data) and really slow timings in real code (because fragmentation forces realloc to move all the data). In contrast, deque append performance is consistent because it never reallocs and never moves data.

2 of 5
48

For what it is worth:

Python 3

deque.pop vs list.pop

> python3 -mtimeit -s 'import collections' -s 'items = range(10000000); base = [*items]' -s 'c = collections.deque(base)' 'c.pop()'
5000000 loops, best of 5: 46.5 nsec per loop 
    
> python3 -mtimeit -s 'import collections' -s 'items = range(10000000); base = [*items]' 'base.pop()'
5000000 loops, best of 5: 55.1 nsec per loop

deque.appendleft vs list.insert

> python3 -mtimeit -s 'import collections' -s 'c = collections.deque()' 'c.appendleft(1)'
5000000 loops, best of 5: 52.1 nsec per loop

> python3 -mtimeit -s 'c = []' 'c.insert(0, 1)'
50000 loops, best of 5: 12.1 usec per loop

Python 2

> python -mtimeit -s 'import collections' -s 'c = collections.deque(xrange(1, 100000000))' 'c.pop()'
10000000 loops, best of 3: 0.11 usec per loop
    
> python -mtimeit -s 'c = range(1, 100000000)' 'c.pop()'
10000000 loops, best of 3: 0.174 usec per loop
   
> python -mtimeit -s 'import collections' -s 'c = collections.deque()' 'c.appendleft(1)'
10000000 loops, best of 3: 0.116 usec per loop
    
> python -mtimeit -s 'c = []' 'c.insert(0, 1)'
100000 loops, best of 3: 36.4 usec per loop

As you can see, where it really shines is in appendleft vs insert.

🌐
DEV Community
dev.to › v_it_aly › python-deque-vs-listwh-25i9
Python: deque vs. list - DEV Community
December 10, 2020 - Internally, deque is a representation of a doubly-linked list. Doubly-linked means that it stores at least two more integers (pointers) with each item, which is why such lists take up more memory space.
🌐
Medium
medium.com › @tihomir.manushev › when-to-ditch-the-list-exploring-pythons-deque-f402e5694aad
When to Ditch the List: Exploring Python’s deque | by Tihomir Manushev | Medium
October 16, 2025 - Learn when and why to replace Python's standard list with the highly efficient collections.deque. This guide covers performance differences, core operations, and practical use cases like queues, fixed-size histories (maxlen), and more, with clear code examples.
🌐
LinkedIn
linkedin.com › pulse › python-deque-vs-list-performance-comparison-prabhat-mishra
Python: Deque vs List performance comparison
May 15, 2021 - Deque is preferred over the list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to list which provides O(n) ...
Find elsewhere
🌐
AskPython
askpython.com › python › list › list-vs-deque-comparison
List vs Deque Performance Comparison - AskPython
April 23, 2023 - So is a deque better than a list? If you have a time constraint, it is better to use deque. But deque lacks functionality. Lists in Python gain their importance through their slicing ability.
🌐
Python Development
pythonista.hashnode.dev › python-deque-vs-list
Python: deque vs. list
December 18, 2020 - Internally, deque is a representation of a doubly-linked list. Doubly-linked means that it stores at least two more integers (pointers) with each item, which is why such lists take up more memory space.
🌐
Real Python
realpython.com › python-deque
Python's deque: Implement Efficient Queues and Stacks – Real Python
January 12, 2026 - Because Python lists provide both operations with the .append() and .pop() methods, you can use them as stacks and queues. However, the performance issues you saw before can significantly impact the overall performance of your applications. Python’s deque was the first data type added to the collections module back in Python 2.4.
🌐
Medium
maciejzalwert.medium.com › python-lists-vs-deques-practical-considerations-and-trade-offs-7b3492054214
Python Lists vs. Deques: Practical Considerations and Trade-offs | by Maciej Zalwert | Medium
April 4, 2024 - I bet you’re mostly using lists in your python projects, which is OK, but not always optimal. In most cases, you can use deques as an alternative to lists.
🌐
Quora
quora.com › What-are-the-pros-and-cons-of-a-list-versus-a-deque-in-Python-Under-what-conditions-should-we-use-a-list-versus-a-deque
What are the pros and cons of a list versus a deque in Python? Under what conditions should we use a list versus a deque? - Quora
Answer (1 of 3): Under the hood, a deque is a doubly-linked list. That means it has s bigger memory footprint than a list, because it stores two pointers per node instead of one. This is usually negligible in practice because the node’s contents will require more memory than a pointer.
🌐
Medium
medium.com › the-pythonworld › most-developers-dont-use-deques-python-s-hidden-super-fast-list-alternative-collections-deque-3ec5c91965b9
Most Developers Don’t Use Deques — Python’s Hidden Super-Fast List Alternative (collections.deque) | by mata | The Pythonworld | Medium
September 10, 2025 - Use lists when you need indexing and random access. Use deques when you need fast appends/pops from both ends. So why don’t developers use deques more often? A few reasons: ... 3. Premature optimization fear: Developers assume they won’t ...
🌐
Quora
quora.com › When-should-I-use-deque-rather-than-list-or-array
When should I use deque rather than list or array? - Quora
Answer (1 of 2): The Double Ended Queue is designed to be faster than a plain array for appending to and removing from either end. It automatically expands to the left or to the right as needed, avoiding the large-scale shifting an array or a vector must do. The following sequence [code]std:de...
Top answer
1 of 1
34

Your mileage may vary according to your application and precise use case, but in the general case, a list is well suited for a stack:

append is O(1)

pop() is O(1) - as long as you do not pop from an arbitrary position; pop() only from the end.

Deques are also O(1) for these operations, but require importing a module from the standard library, require 'O(n)' for random access. An argument could be made to prefer using vanilla lists though, unless for specific applicatins.

Correction:

from this post by Raymond Hettinger, a principal author of the C code for both list and deque, it appears that deques may perform slightly better than lists: The pop() operation of deques seem to have the advantage.

In [1]: from collections import deque

In [2]: s = list(range(1000))      # range(1000) if python 2

In [3]: d = deque(s)

In [4]: s_append, s_pop = s.append, s.pop

In [5]: d_append, d_pop = d.append, d.pop

In [6]: %timeit s_pop(); s_append(None)
10000000 loops, best of 3: 115 ns per loop

In [7]: %timeit d_pop(); d_append(None)
10000000 loops, best of 3: 70.5 ns per loop

the real differences between deques and list in terms of performance are:

Deques have O(1) speed for appendleft() and popleft() while lists have O(n) performance for insert(0, value) and pop(0).
List append performance is hit and miss because it uses realloc() under the hood. As a result, it tends to have over-optimistic timings in simple code (because the realloc doesn't have to move data) and really slow timings in real code (because fragmentation forces realloc to move all the data). In contrast, deque append performance is consistent because it never reallocs and never moves data.

🌐
Medium
medium.com › cloud-for-everybody › stop-using-lists-for-queues-and-stacks-in-python-use-deque-instead-7c9619802ca0
Stop Using Lists for Queues in Python: Use Deque Instead
March 1, 2026 - And the same goes for pop(0), which also requires shifting elements. That’s why these operations get slower as your list grows longer. This is exactly where a deque comes in. In this tutorial, we’ll explore what a Python deque is, how it's different from a list, and how you can use it…
🌐
Reddit
reddit.com › r/learnpython › deque over lists?
r/learnpython on Reddit: deque over lists?
February 7, 2018 -

I found this method while searching for more efficient way to use lists: https://docs.python.org/2/library/collections.html#collections.deque

Its o(1) instead of lists o(n), with this comes mine question. There is a situation where lists are preferred over deque?

Top answer
1 of 1
3
You left out a key detail in you're comparison of O(1) vs O(n), the docs say: Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction. The second paragraph explains the difference: Though list objects support similar operations, they are optimized for fast fixed-length operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which change both the size and position of the underlying data representation. Meaning that purely for adding a value to the beginning of the sequence, deque's are faster, as the name of 'double ended queue' already suggests. This does not mean they have any improvements for other aspects of list operations, which for example this SO answer explains: What this means is that Python lists are much better for random-access and fixed-length operations, including slicing, while deques are much more useful for pushing and popping things off the ends, with indexing (but not slicing, interestingly) being possible but slower than with lists. So only if you actually need the double endedness then a deque would come to mind. Another extra feature for which I use deque's myself is a fifo queue with a maximum length that's enforced by dropping items from the front of the queue. Of course that's a very specific use case that just happens to work for deque's, if it wasn't there then I would implement it myself anyway. Using its rotate() to make it a shift register is another example of this.
🌐
DEV Community
dev.to › wnleao › python-deque-vs-list-time-comparison-5ch4
Python deque vs list: a time comparison - DEV Community
April 10, 2022 - ... Deques are a generalization ... same O(1) performance in either direction. In python, list operations pop from the end and append will also have time complexity O(1)....
🌐
Dataquest
dataquest.io › home › blog › python deque function: a better choice for queues and stacks
Python Deque Function: A Better Choice for Queues and Stacks – Dataquest
April 7, 2025 - If you use Python, you're probably familiar with lists, and you probably use them a lot, too. They're great data structures with many helpful methods that allow the user to modify the list by adding, removing, and sorting items. However, there are some use cases when a list may look like a great choice, but it just isn't. That is where the deque() function (short for double-ended queue, pronounced like "deck") from the collections module can be a much better choice when you need to implement queues and stacks in Python.
🌐
YouTube
youtube.com › watch
Python: List Vs Deque
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Top answer
1 of 2
8

A List is general and flexible.

Generalized structures are ideal for return values in business, web, and desktop applications. It's not optimal to return a Deque or an Array if your consumers will be converting to List in most cases anyway. It's potentially less efficient. And, it clutters your consumers' codebases with boilerplate conversions and/or utility libraries to deal with your "more efficient" interfaces.

To be clear: Some structures, like Deque's and Array's are certainly more efficient for particular use cases, but a List is sufficiently efficient for most use cases. List's may be over-used. But, in many domains, the performance hit of using a List over a more well-suited structure is negligible — not even worth a second thought.

Compound that with work-effort versus business value: Choosing a data structure that supports the precise interface I need and no more requires mental effort and time. And those are costs to the business. Sometimes there's a corresponding increase in value. But, in many applications there isn't — neither the business nor the customers will see any difference if you swap your List out for a more-optimized Deque when you're piping 50 rows from the database through your model to your view. (Or whatever.)

That said, there are use-cases that are well-served by List's.

The "need" for a List is more obvious in long-lived, stateful applications, where collections can stick around and be mutated over a long period of time. A text editor, for example, shouldn't automatically create a data[MAX_ROWS][MAX_COLS] array of arrays. And, you certainly could create an array of arrays with some default sizes and scaling behaviors and add methods for insertions and deletions. But, that's precisely what the List interface already does for you. (And the existing implementation already well-tested and optimized!)

My "2 cents": Unless your domain is small, very well-defined, or performance-critical, start with general collections and optimize later. The flexibility is immediate valuable in most business and web applications. Whereas the performance edge is negligible.

And don't forget about stateful applications, where a List interface is more often precisely what you'll end up reinventing if you've defaulted to an array or Deque...


Examples of things where you might favor efficiency over flexibility might include things like system code (OS, driver, firmware), a VM, or other types of "platform" code — like a modern web browser's internal DOM implementation or JavaScript execution engine.

2 of 2
4

Is there a reason to use java.util.List instead of using java.util.Deque?

Yes.

java.util.List has methods to access elements by position. java.util.Deque does not. If you are implementing an algorithm that needs to access elements by position, then Deque is not going to work. The contrapositive is true as well: If your algorithm only needs to look at the items in sequence (i.e., if you can use an Iterator), and if it only needs to mutate the sequence at the two ends (or one end, or not at all), then a Deque will work for you.

The guiding principle is pretty simple: Choose an interface that defines the methods that your application needs to call.


Is java.util.List a well-designed inteface with a clear purpose?

IMO, No. It tries to be too many different things. If I want you to call my function that will randomly access the list elements, then I am not going to allow you to pass in just any List. I am going to make you pass in an ArrayList, or maybe, if we are collaborating on a project where it makes sense to have a lot of custom classes, we will define an interface that defines exactly those operations that my code is going to perform on your list.