Deque Implementation in Python
Last Updated :
13 Feb, 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 in optimized implementations. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In, First Out) operations.

Deque in Python Library
Deque in Python is implemented using a doubly linked list, making append and pop operations from both ends O(1) (constant time). Unlike Python’s built-in list (which uses a dynamic array), deque does not require shifting elements when inserting/deleting at the front.
Implementing Your Own Deque in Python
Different methods to implement Deque are:
1. Using Doubly Linked List
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. Each node contains three parts:
- Data: Stores the value
- next: Pointer to the next node
- prev: Pointer to the previous node
Operations:
1. Append at the rear (append_rear(item) ): Adds an item at the rear. If the deque is empty, it initializes both front and rear to the new node. Otherwise, it links the current rear to the new node and updates the rear pointer to the new node.
2. Insert at the front (insert_front(item) ): Adds an item at the front. If the deque is empty, it initializes both front and rear to the new node. Otherwise, it links the new node to the current front and updates the front pointer to the new node.
3. Remove from the rear (remove_rear() ): Removes an item from the rear. If the deque has only one item, it sets both front and rear to None. If there are multiple items, it moves the rear pointer to the previous node and updates the next pointer of the new rear to None.
4. Remove from the front (remove_front() ): Removes an item from the front. If the deque has only one item, it sets both front and rear to None. If there are multiple items, it moves the front pointer to the next node and updates the prev pointer of the new front to None.
Time Complexity : All operations (append, insert, remove) are O(1), since adding or removing elements only requires adjusting a few pointers.
Pros of Using a Doubly Linked List for Deque
- Efficient Insertions/Deletions (O(1)): No need to shift elements like in arrays.
- Dynamic Size: No need to predefine a fixed size (unlike circular array-based deque).
- Supports Bi-Directional Traversal: Can traverse from front to rear and rear to front easily.
No Memory Waste: Unlike an array, no extra memory is allocated beforehand.
Challenges of Using a Doubly Linked List for Deque
- Extra Memory for Pointers: Each node requires two extra pointers (next & prev).
- More Complex Implementation: Requires manual memory management and careful handling of pointers.
- Cache Unfriendliness: Unlike an array-based deque, linked lists are not cache-efficient, as elements are stored at non-contiguous memory locations.
To explore in detail refer - Implementation of Deque using Doubly Linked List in Python
2. Using List and Circular List
Python lists can be used to simulate a deque, but since lists are dynamic arrays, operations at the beginning of the list are inefficient because elements need to be shifted. Below are the basic operations:
Operations
1. Append at the rear (list.append(item) → O(1) ): This operation adds an item to the end (rear) of the list. Time Complexity is O(1).
2. Insert at the front (list.insert(0, item) → O(n) ): This operation inserts an item at the front (beginning) of the list. Time Complexity is O(n).
3. Remove from rear (list.pop() → O(1) ): This operation removes and returns the last item from the list (the rear of the deque). Time Complexity is O(1).
4. Remove from front (list.pop(0) → O(n) ): This operation removes and returns the first item from the list (the front of the deque). Time Complexity is O(n).
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.
Pros of Using a Circular List for Deque
- Efficient Insertions/Deletions (O(1)): No shifting is required, making operations faster.
- Bi-Directional Traversal: Can traverse the list in both forward and backward directions efficiently.
- No Fixed Size Limitation: Unlike an array-based deque, it can dynamically grow and shrink.
- Circular Nature Eliminates Edge Cases: No need to handle special cases for front or rear as they wrap around automatically.
- Efficient Memory Utilization: No wasted memory slots, as seen in array-based implementations.
Challenges of Using a Circular List for Deque
- Extra Memory Overhead: Each node requires two extra pointers (next & prev), increasing memory usage.
- Complex Implementation: Managing circular links correctly requires careful handling, especially during insertions and deletions.
- Cache Inefficiency: Unlike array-based deques, linked lists suffer from poor cache locality, affecting performance in large datasets.
- Higher Maintenance Cost: Requires manual pointer management to prevent dangling pointers and memory leaks in languages without garbage collection.
To explore in detail refer - Implementation of Deque Using List in Python
Similar Reads
Deque in Python A deque stands for Double-Ended Queue. It is a data structure that allows adding and removing elements from both ends efficiently. Unlike regular queues, which are typically operated on using FIFO (First In, First Out) principles, a deque supports both FIFO and LIFO (Last In, First Out) operations.E
6 min read
Python Built in Functions Python is the most popular programming language created by Guido van Rossum in 1991. It is used for system scripting, software development, and web development (server-side). Web applications can be developed on a server using Python. Workflows can be made with Python and other technologies. Databas
6 min read
Deque vs List in Python Python provides multiple data structures to handle collections of data, with list and deque (double-ended queue) being two commonly used structures, each offering unique advantages depending on the operation at hand.List is a dynamic array that supports indexing and slicing.Deque is a doubly linked
4 min read
Internal implementation of Data Structures in Python Python provides a variety of built-in data structures, each with its own characteristics and internal implementations optimized for specific use cases. In this article we are going to discuss about the most commonly used Data structures in Python and a brief overview of their internal implementation
3 min read
Dictionaries in Python Python dictionary is a data structure that stores the value in key: value pairs. Values in a dictionary can be of any data type and can be duplicated, whereas keys can't be repeated and must be immutable. Example: Here, The data is stored in key:value pairs in dictionaries, which makes it easier to
5 min read
IndexError: pop from Empty Deque in Python In Python, the IndexError: pop from an empty deque is an error that occurs when trying to use the pop() method on an empty deque. A deque (double-ended queue) is a versatile data structure that allows efficient addition and removal of elements from both ends. This article explores the causes of the
4 min read
Data Abstraction in Python Data abstraction is one of the most essential concepts of Python OOPs which is used to hide irrelevant details from the user and show the details that are relevant to the users. For example, the readers of geeksforgeeks only know that a writer can write an article on geeksforgeeks, and when it gets
5 min read
Python Functions Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
9 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read