Implementation of Deque Using List in Python
Last Updated :
12 Feb, 2025
A Deque (double-ended queue) is a data structure that allows insertion and deletion from both the front and rear. Deques are widely used in applications requiring efficient access and modification at both ends. While Python provides a built-in collections.deque for optimized performance, a deque can also be implemented using lists.

Operations on Deque
1. Insertion at Rear (insertRear(x)
): Adds an item at the rear of Deque.
- Checks if deque is full.
- Calculates the new rear index using (front + size) % cap.
- Inserts the element at the computed rear index.
- Increments the deque size.
2. Insertion at Front (insertFront(x)
): Adds an item at the front of Deque.
- Checks if deque is full.
- Updates
front
index using (front - 1) % cap. - Inserts the element at the new front index.
- Increments the deque size.
3. Deletion from Front (deleteFront()
): Deletes an item from front of Deque.
- Checks if deque is empty.
- Retrieves and returns the front element.
- Updates
front
index cyclically. - Decreases the deque size.
4. Deletion from Rear (deleteRear()
): Deletes an item from rear of Deque.
- Checks if deque is empty.
- Computes the rear index using (front + size - 1) % cap.
- Retrieves and returns the rear element.
- Decreases the deque size.
5. Displaying Deque (display()
): Displays the Deque
- Checks if deque is empty and returns an empty list if true.
- Iterates over the deque in a circular manner and returns the current elements.
Deque Implementation Using a List
1. Naive Implementation
The idea is to implement insert and delete operations by inserting and removing elements at both the ends. The insert and remove operations at the end are O(1) on average, but the same operations at beginning take O(n) time as we need to shift elements.
Python
class ListDeque:
def __init__(self):
self.deque = []
def insert_front(self, item):
self.deque.insert(0, item)
def insert_rear(self, item):
self.deque.append(item)
def remove_front(self):
if self.deque:
return self.deque.pop(0)
return None
def remove_rear(self):
if self.deque:
return self.deque.pop()
return None
def display(self):
return self.deque
# Example Usage
dq = ListDeque()
dq.insert_front(10)
dq.insert_rear(20)
print(dq.display())
print(dq.remove_front())
2. Efficient Implementation - Circular Approach
A deque (double-ended queue) allows inserting and deleting elements from both the front and rear. However, using a normal Python list makes front operations inefficient because shifting elements takes O(n) time. To overcome this, we use a circular array approach, where the deque operates in a fixed-size list, and elements wrap around when they reach the end.
1. Efficient Insertions & Deletions
- Rear insertion is done at (front + size) % capacity, ensuring the elements wrap around.
- Front insertion moves the front pointer backward using (front - 1) % capacity.
- Deletions adjust the front or rear index dynamically, avoiding unnecessary shifting.
2. Fixed Capacity & Wrapping Around
- When inserting, we don't shift elements; instead, we update indices using the modulus operator (%).
- This ensures that if the deque reaches the end of the array, it wraps around to the beginning.
3. Displaying Elements
- Since the deque is circular, elements are accessed using modulus arithmetic (front + i) % capacity to print them in the correct order.
Python
class MyDeque:
def __init__(self, c):
self.l = [None] * c
self.cap = c
self.size = 0
self.front = 0
def insertRear(self, x):
# Check if deque is full
if self.size == self.cap:
return
else:
# Calculate new rear index
rear = (self.front + self.size) % self.cap
self.l[rear] = x
self.size += 1
def insertFront(self, x):
# Check if deque is full
if self.size == self.cap:
return
else:
self.front = (self.front - 1) % self.cap
self.l[self.front] = x
self.size += 1
def deleteFront(self):
# Check if deque is empty
if self.size == 0:
return None
else:
res = self.l[self.front]
self.front = (self.front + 1) % self.cap
self.size -= 1
return res
def deleteRear(self):
# Check if deque is empty
if self.size == 0:
return None
else:
rear = (self.front + self.size - 1) % self.cap
res = self.l[rear]
self.size -= 1
return res
def display(self):
if self.size == 0:
return []
else:
result = []
for i in range(self.size):
index = (self.front + i) % self.cap
result.append(self.l[index])
return result
# Example Usage
d = MyDeque(4)
d.insertRear(10)
d.insertFront(20)
d.insertFront(30)
print(d.display())
d.deleteFront()
print(d.display())
d.deleteRear()
print(d.display())
Output[30, 20, 10]
[20, 10]
[20]
Please refer Implementation of Deque using Doubly Linked List in Python for another implementation with time complexity of all operations as O(1).
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read