A deque is a generalization of stack and a queue (It is short for "double-ended queue").

Thus, the pop() operation still causes it to act like a stack, just as it would have as a list. To make it act like a queue, use the popleft() command. Deques are made to support both behaviors, and this way the pop() function is consistent across data structures. In order to make the deque act like a queue, you must use the functions that correspond to queues. So, replace pop() with popleft() in your second example, and you should see the FIFO behavior that you expect.

Deques also support a max length, which means when you add objects to the deque greater than the maxlength, it will "drop" a number of objects off the opposite end to maintain its max size.

Answer from James on Stack Overflow
🌐
Python
docs.python.org › 3 › library › collections.html
collections — Container datatypes
A round-robin scheduler can be implemented with input iterators stored in a deque. Values are yielded from the active iterator in position zero. If that iterator is exhausted, it can be removed with popleft(); otherwise, it can be cycled back to the end with the rotate() method:
🌐
GeeksforGeeks
geeksforgeeks.org › python › deque-in-python
Deque in Python - GeeksforGeeks
A deque also supports indexing, which means elements can be accessed using their position just like lists.
Published   May 29, 2026
🌐
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.
🌐
Real Python
realpython.com › python-deque
Python's deque: Implement Efficient Queues and Stacks – Real Python
January 12, 2026 - Python’s deque returns mutable sequences that work quite similarly to lists. Besides allowing you to append and pop items from their ends efficiently, deques provide a group of list-like methods and other sequence-like operations to work with items at arbitrary locations.
🌐
CodeSignal
codesignal.com › learn › courses › advanced-built-in-data-structures-and-their-usage › lessons › understanding-queues-and-deques-in-python
Understanding Queues and Deques in Python
A deque, or "double-ended queue", ... for implementing deques. We can add items to both ends of our deque using the append(item) method for the right end and the appendleft(item) method for the left....
🌐
Codecademy
codecademy.com › docs › python › deque
Python | Deque | Codecademy
April 10, 2025 - A deque is a double-ended queue implementation in Python’s collections module. It provides a versatile data structure that generalizes a stack and a queue by allowing efficient append and pop operations from both ends of the sequence.
🌐
Mathspp
mathspp.com › blog › python-deque-tutorial
Python deque tutorial | mathspp
January 18, 2024 - This tokenizer has a method next_token that computes and emits the next token: class Tokenizer: # ... def next_token(self) -> Token: # ... The tokenizer has a token buffer implemented with a deque. If the buffer has any tokens when Tokenizer.next_token is called, we pop a token from the left of the buffer instead of computing the next one: from collections import deque class Tokenizer: def __init__(self, code: str) -> None: # ...
Find elsewhere
Top answer
1 of 2
23

A deque is a generalization of stack and a queue (It is short for "double-ended queue").

Thus, the pop() operation still causes it to act like a stack, just as it would have as a list. To make it act like a queue, use the popleft() command. Deques are made to support both behaviors, and this way the pop() function is consistent across data structures. In order to make the deque act like a queue, you must use the functions that correspond to queues. So, replace pop() with popleft() in your second example, and you should see the FIFO behavior that you expect.

Deques also support a max length, which means when you add objects to the deque greater than the maxlength, it will "drop" a number of objects off the opposite end to maintain its max size.

2 of 2
3

I'll add my two cents as I was searching for this exact question but more from the time complexity involved and what should be the preferred choice for a queue implementation in Python.

As per the docs:

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.

This means you can use dequeues as a stack(Last in First out) and queue(First in First out) implementation with pop() or popleft() operation in O(1).

Again from docs

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.

However, using the list as a queue requires popping from the 0th index which will cause data to be shifted resulting in O(N) operation. So if you want to use a queue for a time sensitive operation (production code or competitive programming) always use dequeue for queue implementation.

🌐
FavTutor
favtutor.com › blogs › deque-python
Python Deque: Example, Implementation & Methods (with ...
5 days ago - Learn the Python deque from collections: how to import and create one, all deque methods, O(1) appends and pops at both ends, maxlen windows, and rotate().
🌐
Medium
medium.com › @codingcampus › deque-in-python-34a02ad0e498
Deque in Python. A Deque is a data structure in the… | by CodingCampus | Medium
November 23, 2023 - A Deque is a data structure in the Python collection module that allows fast append and pop operations. In programming, we tend to deal…
🌐
Medium
khambud.medium.com › deque-data-structure-96640031d4b0
Deque Data Structure
May 21, 2023 - In conclusion, deque functions as both a queue and a stack. It has efficient append and pop methods.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › deque in python
Deque in Python | How Deque works in Python with Examples?
April 12, 2023 - Basically, it is a part of the collection library; in deque, we can add or remove the elements from both ends that we call append and pop operation. In deque, every append operation provides the 0(1) tie complexity and every pop operation provides ...
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Runestone Academy
runestone.academy › ns › books › published › pswadsup › basic-ds_implementing-a-deque-in-python.html
DS Implementing a Deque in Python
class Deque: """Deque implementation as a list""" def __init__(self): """Create new deque""" self._items = [] def is_empty(self): """Check if the deque is empty""" return not bool(self._items) def add_front(self, item): """Add an item to the front of the deque""" self._items.append(item) def add_rear(self, item): """Add an item to the rear of the deque""" self._items.insert(0, item) def remove_front(self): """Remove an item from the front of the deque""" return self._items.pop() def remove_rear(self): """Remove an item from the rear of the deque""" return self._items.pop(0) def size(self): """Get the number of items in the deque""" return len(self._items) In remove_front we use the pop method to remove the last element from the list.
🌐
OpenGenus
iq.opengenus.org › deque-python
Deque in Python
March 20, 2020 - Deque (double ended queue) is a data structure that can be used to insert or delete data elements at both it ends. It is directly supported in Python through collections module.
🌐
CodeConverter
codeconverter.com › articles › deque-python
Deque Python — Guide with Examples | CodeConverter Blog
February 11, 2026 - Deque provides two methods for removing elements: `pop()` and `popleft()`. You guessed it — they remove from the right and left sides respectively.
🌐
The Teclado Blog
blog.teclado.com › python-deques
Python Deques
June 24, 2019 - Deques are a very handy collection type from the built-in "collections" module. They extend the functionality of lists and give us a couple more useful methods. Learn about them in this post!
🌐
Python
bugs.python.org › issue3891
Issue 3891: collections.deque should have empty() method - Python tracker
September 17, 2008 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/48141
🌐
Career Karma
careerkarma.com › blog › python › python queue and deque: a step-by-step guide
Python Queue and Deque: A Step-By-Step Guide | Career Karma
December 1, 2023 - Python queue is a built in library that allows you to create a list that uses the FIFO rule, first in first out. Python deque uses the opposite rule, LIFO queue, or last in first out.
🌐
GeeksforGeeks
geeksforgeeks.org › python › deque-implementation-in-python
Deque Implementation in Python - GeeksforGeeks
July 23, 2025 - A deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear in O(1) time. Python’s collections.deque is implemented using a doubly linked list.
🌐
Laurentluce
laurentluce.com › posts › python-deque-implementation
Python deque implementation – Laurent Luce's Blog
static PyObject * deque_popleft(dequeobject *deque, PyObject *unused) { ... if (deque->leftindex == BLOCKLEN) { if (Py_SIZE(deque)) { prevblock = deque->leftblock->rightlink; freeblock(deque->leftblock); deque->leftblock = prevblock; deque->leftindex = 0; } ... } ... } We looked at what happens when we append to the end of the queue (append right) and when we pop from the beginning of the queue (pop left). The methods appendleft and pop (pop right) have similar internal mechanisms.