🌐
GeeksforGeeks
geeksforgeeks.org › python › deque-in-python
Deque in Python - GeeksforGeeks
Examples · Quizzes · DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 29 May, 2026 · A deque stands for Double-Ended Queue. It is a type of data structure that allows to add and remove elements from both ends efficiently.
Published   May 29, 2026
🌐
Real Python
realpython.com › python-deque
Python's deque: Implement Efficient Queues and Stacks – Real Python
January 12, 2026 - Your final example emulates the tail command, which is available on Unix-like operating systems. The command accepts a file path and prints the last ten lines of that file to the system’s standard output. You can tweak the number of lines you need tail to print with the -n, --lines option. Here’s a small Python function that emulates the core functionality of tail: ... >>> from collections import deque >>> def tail(filename, lines=10): ...
🌐
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().
🌐
Mathspp
mathspp.com › blog › python-deque-tutorial
Python deque tutorial | mathspp
January 18, 2024 - This tutorial teaches how to work with the Python data structure collections.deque and provides 7 example use cases.
🌐
Python
docs.python.org › 3 › library › collections.html
collections — Container datatypes
The rotate() method provides a way to implement deque slicing and deletion. For example, a pure Python implementation of del d[n] relies on the rotate() method to position elements to be popped:
🌐
Tutorialspoint
tutorialspoint.com › python › python_deque.htm
Python - Deque
import collections # Create a deque DoubleEnded = collections.deque(["Mon","Tue","Wed"]) print (DoubleEnded) # Append to the right print("Adding to the right: ") DoubleEnded.append("Thu") print (DoubleEnded) # append to the left print("Adding to the left: ") DoubleEnded.appendleft("Sun") print (DoubleEnded) # Remove from the right print("Removing from the right: ") DoubleEnded.pop() print (DoubleEnded) # Remove from the left print("Removing from the left: ") DoubleEnded.popleft() print (DoubleEnded) # Reverse the dequeue print("Reversing the deque: ") DoubleEnded.reverse() print (DoubleEnded)
🌐
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.
🌐
Python Module of the Week
pymotw.com › 2 › collections › deque.html
Deque - Python Module of the Week
Since deques are a type of sequence ... and removing elements from the middle by matching identity. $ python collections_deque.py Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g']) Length: 7 Left end: a Right end: g remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])...
🌐
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 - For example, we can use len() to check the size of a deque: ... Now, let's put a simplified version of a queue into practice. We'll keep using the example of a queue of songs, which means our queue will keep receiving new songs as it plays the ...
Find elsewhere
🌐
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 - #number = deque ([iterable], maxlen)number = deque ([1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 9]) sentence = deque (['hello', "hey", "hi"])#extendleft(iterable)print(number + sentence)[/python] ... We added two deques with concatenation.
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.

🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Use Deque in Python: collections.deque | note.nkmk.me
April 20, 2025 - In Python, the collections.deque class provides an efficient way to handle data as a queue, stack, or deque (double-ended queue). collections - deque objects — Container datatypes — Python 3.13.3 do ...
🌐
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. A Doubly Linked List (DLL) allows efficient insertion and deletion at both ends in O(1) time.
🌐
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", allows the addition and removal of items from both ends. Python provides the collections module containing the deque class for implementing deques.
🌐
Pythontic
pythontic.com › containers › deque › introduction
The deque in Python | Pythontic.com
The deque class is a double ended queue. Elements can be added to and removed from either side of a deque object. This way deque acts as a queue as well as a stack.
🌐
Hackr
hackr.io › home › articles › programming
How to Use a Python Deque for Fast and Efficient Queues
January 30, 2025 - Input-restricted deques are often used in applications where we require a queue, but the ordering of the elements is not important. For example, we could use an input-restricted deque to store CPU tasks that require processing.
🌐
Allendowney
allendowney.github.io › DSIRP › deque.html
Deque — Data Structures and Information Retrieval in Python
You can use the following examples to test your function. deque = Deque() left_push(deque, 2) left_push(deque, 1) print(deque) ... The Python collections module provides an implementation of a deque.
🌐
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 - By using deque, we can create a double-ended queue where we can add and remove elements from the start or end of the queue. Deques are last-in, first-out, or LIFO for short. Let’s use the same example as above: storing waitlisted names for a movie.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python deque methods
Python Deque Methods - Spark By {Examples}
May 31, 2024 - In Python we have several deque methods, and using these methods we can implement deques very efficiently. The deque stands for Double Ended Queue
🌐
Laurentluce
laurentluce.com › posts › python-deque-implementation
Python deque implementation – Laurent Luce's Blog
A linked list with a single block of 64 pointers (set to null) is created. The right index is initialized to the center of the block (31) and is used to refer to the last object in the deque. The left index is initialized to the center of the block plus one (32) and is used to refer to the ...