(Translated by https://www.hiragana.jp/)
Deque in Python - GeeksforGeeks
Open In App

Deque in Python

Last Updated : 21 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Example:


Output
deque(['name', 'age', 'DOB'])
Deque

Why Do We Need deque

  • It supports O(1) time for adding/removing elements from both ends.
  • It is more efficient than lists for front-end operations.
  • It can function as both a queue (FIFO) and a stack (LIFO).
  • Ideal for scheduling, sliding window problems and real-time data processing.
  • It offers powerful built-in methods like appendleft(), popleft() and rotate().

Types of Restricted Deque Input

  • Input Restricted Deque:  Input is limited at one end while deletion is permitted at both ends.
  • Output Restricted Deque: output is limited at one end but insertion is permitted at both ends.

Operations on deque 

Here’s a table listing built-in operations of a deque in Python with descriptions and their corresponding time complexities:

OperationDescriptionTime Complexity
append(x)Adds x to the right end of the deque.O(1)
appendleft(x)Adds x to the left end of the deque.O(1)
pop()Removes and returns an element from the right end of the deque.O(1)
popleft()Removes and returns an element from the left end of the deque.O(1)
extend(iterable)Adds all elements from iterable to the right end of the deque.O(k)
extendleft(iterable)Adds all elements from iterable to the left end of the deque (reversed order).O(k)
remove(value)Removes the first occurrence of value from the deque. Raises ValueError if not found.O(n)
rotate(n)Rotates the deque n steps to the right. If n is negative, rotates to the left.O(k)
clear()Removes all elements from the deque.O(n)
count(value)Counts the number of occurrences of value in the deque.O(n)
index(value)Returns the index of the first occurrence of value in the deque. Raises ValueError if not found.O(n)
reverse()Reverses the elements of the deque in place.O(n)

Appending and Deleting Dequeue Items

  • append(x): Adds x to the right end of the deque.
  • appendleft(x): Adds x to the left end of the deque.
  • extend(iterable): Adds all elements from the iterable to the right end.
  • extendleft(iterable): Adds all elements from the iterable to the left end (in reverse order).
  • remove(value): Removes the first occurrence of the specified value from the deque. If value is not found, it raises a ValueError.
  • pop(): Removes and returns an element from the right end.
  • popleft(): Removes and returns an element from the left end.
  • clear(): Removes all elements from the deque.

Output:

After extend([50, 60, 70]): deque([5, 10, 20, 30, 40, 50, 60, 70])
After extendleft([0, 5]): deque([5, 0, 5, 10, 20, 30, 40, 50, 60, 70])
After remove(20): deque([5, 0, 5, 10, 30, 40, 50, 60, 70])
After pop and popleft: deque([0, 5, 10, 30, 40, 50, 60])
After clear(): deque([])

Accessing Item and length of deque

  • Indexing: Access elements by position using positive or negative indices.
  • len(): Returns the number of elements in the deque.

Output
1
4
7

Count, Rotation and Reversal of a deque

  • count(value): This method counts the number of occurrences of a specific element in the deque.
  • rotate(n): This method rotates the deque by n steps. Positive n rotates to the right and negative n rotates to the left.
  • reverse(): This method reverses the order of elements in the deque.

Output
3
2
deque([30, 20, 10, 20, 30, 40, 50, 20])
deque([20, 30, 40, 50, 20, 30, 20, 10])
deque([10, 20, 30, 20, 50, 40, 30, 20])

Deque in Python
Visit Course explore course icon

Similar Reads