Featured Article

Deque Python Example: Master Double-Ended Queues Efficiently

Nicole Jul 13, 2026

A deque, short for 'double-ended queue', is a type of data structure in Python that is implemented to allow adding or removing items from both ends of the sequence. While Python's built-in list data type is great and flexible, it's not optimized for fast append and pop operations from both ends. That's where deque comes into play, offered by Python's 'collections' module, which provides an interface with O(1) time complexity for push and pop operations from both ends of the sequence.

Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners

In this guide, we'll walk you through deque in Python, its implementation, and its practical usage. Focusing on real-world examples, we'll demonstrate how you can leverage deque for improved performance and elegant code design.

three different types of python programming
three different types of python programming

Fundamentals of Deque in Python

Before delving into the nitty-gritty of deques, let's lay a solid foundation. A deque in Python is an ordered collection where elements can be added or removed from either end. It's particularly helpful in situations where you require fast appends and pops – for example, in multiprocessing tasks or when working with infinite generators.

Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews

Deques in Python are implemented as doubly-linked lists, different from the dynamic arrays used in lists. Thus, adding or removing elements doesn't require resizing the underlying array, leading to faster performance.

Constructing a deque in Python

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

To construct a deque, you pass an iterable (e.g., a list, a tuple, another deque) to the collections.deque() constructor:

from collections import deque d = deque([1,2,3,4,5])

The output will be deque([1, 2, 3, 4, 5]). Empty deques can also be initialized as follows:

d = deque()

Basic Methods of a deque in Python

Dice in python
Dice in python

Python's deque comes equipped with various methods for manipulating elements. Here are two crucial methods:

  • append(): Adds an element to the right side of the deque.
  • appendleft(): Adds an element to the left side of the deque.

Both afford O(1) time complexity, leading to fast performance during frequent appending operations.

a diagram showing the different types of python's programming skills and their uses in web design
a diagram showing the different types of python's programming skills and their uses in web design

In-Depth Use Cases for deque in Python

Now that you have a good understanding of what a deque is and how to create one, let's use them in practical scenarios to demonstrate their power.

How to Deque in Python?
How to Deque in Python?
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
Functions in Python | Types, Example
Functions in Python | Types, Example
the top 50 python project ideas
the top 50 python project ideas
Python Notes for Beginners (Easy + Quick Guide)
Python Notes for Beginners (Easy + Quick Guide)
PHYTHON PROJECT IDEAS
PHYTHON PROJECT IDEAS
100 Python Project for beginners, intermediate and advanced programmers
100 Python Project for beginners, intermediate and advanced programmers
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)

Queue-like Interface

Since deques support adding elements from both ends while maintaining an ordered collection, they can serve as a queue. Here's an example using a deque for a custom queue:

d = deque() d.append('Item 1') d.appendleft('Item 2') # Now d equals to deque(['Item 2', 'Item 1']) d.popleft() # Removes 'Item 2' from the left, resulting in deque(['Item 1'])

Doubling as a Stack

Similarly, deques can also serve as stacks where adding and removing elements follow the Last-In-First-Out (LIFO) principle. Here's an example

d = deque('abcde') # d equals to deque(['a', 'b', 'c', 'd', 'e']) d.append('Extra') # Adds 'Extra' to the right, resulting in deque(['a', 'b', 'c', 'd', 'e', 'Extra']) d.pop() # Removes 'Extra' from the right; d now equals to deque(['a', 'b', 'c', 'd', 'e'])

Remember, deque can balance both FIFO and LIFO operations, offering a great deal of flexibility to your code.

Embracing deque in Python can significantly boost the performance of your code, particularly when dealing with frequent appends and pops. Economizing data structure manipulation can enhance your code's sustainability, making it a vital tool for Python developers. Perhaps now is the perfect time to harness the power of deque in your next Python project.