Sitemap
M Haque

M Haque

Deque in Python

3 min readDec 27, 2022

A deque, also known as a double-ended queue, is a data structure that allows you to add and remove elements from both the front and the back of the queue. In Python, you can use the collections module to access a deque object. Here are some examples of how you can use a deque in Python:

  1. Adding elements to the deque: You can use the append() method to add an element to the end of the deque, and the appendleft() method to add an element to the front of the deque. For example:
from collections import deque

# Create a deque with initial elements 1, 2, 3
d = deque([1, 2, 3])

# Add element 4 to the end of the deque
d.append(4)
print(d) # Output: deque([1, 2, 3, 4])

# Add element 0 to the front of the deque
d.appendleft(0)
print(d) # Output: deque([0, 1, 2, 3, 4])

2. Removing elements from the deque: You can use the pop() method to remove and return the last element from the deque, and the popleft() method to remove and return the first element from the deque. For example:

from collections import deque

# Create a deque with initial elements 1, 2, 3, 4
d = deque([1, 2, 3, 4])

# Remove and return the last element from the deque
last_element = d.pop()
print(last_element) # Output: 4
print(d) # Output: deque([1, 2, 3])

# Remove and return the first element from the deque
first_element = d.popleft()
print(first_element) # Output: 1
print(d) # Output: deque([2, 3])

3. Rotating the deque: You can use the rotate() method to move elements from the front of the deque to the back, or vice versa. For example:

from collections import deque

# Create a deque with initial elements 1, 2, 3, 4
d = deque([1, 2, 3, 4])

# Rotate the deque by 2 elements to the right
d.rotate(2)
print(d) # Output: deque([3, 4, 1, 2])

# Rotate the deque by 2 elements to the left
d.rotate(-2)
print(d) # Output: deque([1, 2, 3, 4])

4. Iterating over the deque: You can use a for loop to iterate over the elements in the deque. For example:

from collections import deque

# Create a deque with initial elements 1, 2, 3, 4
d = deque([1, 2, 3, 4])

# Iterate over the elements in the deque
for element in d:
print(element) # Output: 1, 2, 3, 4

5. To limit the size of a deque in Python, you can use the maxlen parameter when creating the deque. This will set a maximum size for the deque, and if you try to add more elements to the deque when it is at its maximum size, the oldest element will be automatically removed to make room for the new element.

Get M Haque’s stories in your inbox

Join Medium for free to get updates from this writer.

Here is an example of using the maxlen parameter to limit the size of a deque to 10 elements:

from collections import deque

# Create a deque with a maximum size of 10
d = deque(maxlen=10)

# Add some elements to the deque
d.extend([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(d) # deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)

# The deque is now at its maximum size, so adding another element will cause the oldest element to be removed
d.append(11)
print(d) # deque([2, 3, 4, 5, 6, 7, 8, 9, 10, 11], maxlen=10)

# You can also use the rotate() method to remove the oldest elements and add new ones to the other end of the deque
d.rotate(1)
print(d) # deque([11, 2, 3, 4, 5, 6, 7, 8, 9, 10], maxlen=10)

We discussed a few examples of using deque() in Python.

--

--