Last Updated : 06 Mar, 2025
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: Python
from collections import deque
# Declaring deque
de = deque(['name','age','DOB'])
print(de)
deque(['name', 'age', 'DOB'])Deque in Python Types of Restricted Deque Input
Here’s a table listing built-in operations of a deque in Python with descriptions and their corresponding time complexities:
Operation Description Time Complexity append(x) Addsx
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
from collections import deque
dq = deque([10, 20, 30])
# Add elements to the right
dq.append(40)
# Add elements to the left
dq.appendleft(5)
# extend(iterable)
dq.extend([50, 60, 70])
print("After extend([50, 60, 70]):", dq)
# extendleft(iterable)
dq.extendleft([0, 5])
print("After extendleft([0, 5]):", dq)
# remove method
dq.remove(20)
print("After remove(20):", dq)
# Remove elements from the right
dq.pop()
# Remove elements from the left
dq.popleft()
print("After pop and popleft:", dq)
# clear() - Removes all elements from the deque
dq.clear() # deque: []
print("After clear():", dq)
Output:
After extend([50, 60, 70]): deque([5, 10, 20, 30, 40, 50, 60, 70])Accessing Item and length of deque
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([])
import collections
dq = collections.deque([1, 2, 3, 3, 4, 2, 4])
# Accessing elements by index
print(dq[0])
print(dq[-1])
# Finding the length of the deque
print(len(dq))
Count, Rotation and Reversal of a deque
from collections import deque
# Create a deque
dq = deque([10, 20, 30, 40, 50, 20, 30, 20])
# 1. Counting occurrences of a value
print(dq.count(20)) # Occurrences of 20
print(dq.count(30)) # Occurrences of 30
# 2. Rotating the deque
dq.rotate(2) # Rotate the deque 2 steps to the right
print(dq)
dq.rotate(-3) # Rotate the deque 3 steps to the left
print(dq)
# 3. Reversing the deque
dq.reverse() # Reverse the deque
print(dq)
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])
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4