deque

Introduction

The deque (pronounced deck) is a specialized container provided by Python’s built-in collections module. The name stands for Double-Ended Queue.

Unlike a regular list, a deque allows fast insertion and deletion of elements from both the beginning and the end of the collection.

deque is widely used in:

  • Queue Implementations

  • Stack Implementations

  • Browser History

  • Task Scheduling

  • Automation Frameworks

  • Data Processing Pipelines

  • Real-Time Applications

In this tutorial, you will learn what deque is, how it works, practical examples, automation testing use cases, common mistakes, and best practices.


What is deque?

A deque is a double-ended queue that allows efficient addition and removal of elements from both ends.

Import Syntax

from collections import deque

Why Use deque?

deque provides:

  • Fast insertion at the beginning

  • Fast insertion at the end

  • Fast deletion from both ends

  • Queue functionality

  • Stack functionality


Creating a deque

Example

from collections import deque

numbers = deque([1, 2, 3, 4])

print(numbers)

Output

deque([1, 2, 3, 4])

Adding Elements to the Right

Use append().

Example

from collections import deque

numbers = deque([1, 2, 3])

numbers.append(4)

print(numbers)

Output

deque([1, 2, 3, 4])

Adding Elements to the Left

Use appendleft().

Example

from collections import deque

numbers = deque([2, 3, 4])

numbers.appendleft(1)

print(numbers)

Output

deque([1, 2, 3, 4])

Removing Elements from the Right

Use pop().

Example

from collections import deque

numbers = deque([1, 2, 3, 4])

numbers.pop()

print(numbers)

Output

deque([1, 2, 3])

Removing Elements from the Left

Use popleft().

Example

from collections import deque

numbers = deque([1, 2, 3, 4])

numbers.popleft()

print(numbers)

Output

deque([2, 3, 4])

Extending a deque

Use extend() to add multiple elements to the right.

Example

from collections import deque

numbers = deque([1, 2])

numbers.extend([3, 4, 5])

print(numbers)

Output

deque([1, 2, 3, 4, 5])

Extending from the Left

Use extendleft().

Example

from collections import deque

numbers = deque([3, 4])

numbers.extendleft([2, 1])

print(numbers)

Output

deque([1, 2, 3, 4])

Note: Elements are added in reverse order.


Rotating a deque

Use rotate().

Example

from collections import deque

numbers = deque([1, 2, 3, 4])

numbers.rotate(1)

print(numbers)

Output

deque([4, 1, 2, 3])

The last element moves to the front.


Rotate Left

Example

from collections import deque

numbers = deque([1, 2, 3, 4])

numbers.rotate(-1)

print(numbers)

Output

deque([2, 3, 4, 1])

Counting Elements

Use count().

Example

from collections import deque

numbers = deque([1, 2, 2, 3])

print(numbers.count(2))

Output

2

Finding Element Index

Use index().

Example

from collections import deque

numbers = deque([10, 20, 30])

print(numbers.index(20))

Output

1

Reversing a deque

Use reverse().

Example

from collections import deque

numbers = deque([1, 2, 3])

numbers.reverse()

print(numbers)

Output

deque([3, 2, 1])

Using deque as a Queue (FIFO)

FIFO = First In, First Out

Example

from collections import deque

queue = deque()

queue.append("Task1")
queue.append("Task2")
queue.append("Task3")

print(queue.popleft())

Output

Task1

The first inserted element is removed first.


Using deque as a Stack (LIFO)

LIFO = Last In, First Out

Example

from collections import deque

stack = deque()

stack.append("Page1")
stack.append("Page2")
stack.append("Page3")

print(stack.pop())

Output

Page3

The last inserted element is removed first.


Limiting deque Size

Use maxlen.

Example

from collections import deque

numbers = deque(maxlen=3)

numbers.append(1)
numbers.append(2)
numbers.append(3)
numbers.append(4)

print(numbers)

Output

deque([2, 3, 4], maxlen=3)

The oldest element is automatically removed.


Real-World Example: Browser History

Example

from collections import deque

history = deque(maxlen=5)

history.append("Google")
history.append("YouTube")
history.append("GitHub")

print(history)

Output

deque([
    'Google',
    'YouTube',
    'GitHub'
])

Real-World Example: Task Scheduler

Example

from collections import deque

tasks = deque()

tasks.append("Login Test")
tasks.append("Checkout Test")

print(tasks.popleft())

Output

Login Test

deque in Selenium Automation

Example

from collections import deque

test_queue = deque()

test_queue.append("Login Test")
test_queue.append("Search Test")
test_queue.append("Payment Test")

while test_queue:
    print(
        test_queue.popleft()
    )

Output

Login Test
Search Test
Payment Test

Useful for managing execution queues.


deque in API Automation

Example

from collections import deque

api_requests = deque()

api_requests.append("GET Users")
api_requests.append("POST User")
api_requests.append("DELETE User")

print(
    api_requests.popleft()
)

Output

GET Users

Useful for processing requests in order.


Common Mistakes Beginners Make

Forgetting to Import deque

Incorrect

data = deque()

Error

NameError:
deque is not defined

Correct

from collections import deque

Using List for Large Queue Operations

Inefficient

queue.pop(0)

This is slower for large datasets.


Better

queue.popleft()

Use deque for queue operations.


Misunderstanding extendleft()

Example

from collections import deque

data = deque([3, 4])

data.extendleft([1, 2])

print(data)

Output

deque([2, 1, 3, 4])

Elements are added in reverse order.


Best Practices

Use deque for Queue Operations

queue.append(item)
queue.popleft()

Use deque for Stack Operations

stack.append(item)
stack.pop()

Use maxlen for Fixed-Size Data

deque(maxlen=100)

Prefer deque Over List for Frequent Insertions/Deletions

Especially at the beginning of a collection.


Use Meaningful Variable Names

Examples:

task_queue
browser_history
api_requests
test_results

Advantages of deque

  • Fast insertions and deletions

  • Efficient queue implementation

  • Efficient stack implementation

  • Supports fixed-size collections

  • Memory efficient


Limitations of deque

  • Random access is slower than lists

  • Not ideal for heavy indexing operations

  • Less commonly used than lists for simple tasks


deque vs List

Feature deque List
Append Right Fast Fast
Pop Right Fast Fast
Append Left Fast Slow
Pop Left Fast Slow
Queue Operations Excellent Poor
Random Access Moderate Fast

Conclusion

The deque class from Python’s collections module is a powerful and efficient data structure for handling queues, stacks, and double-ended operations. It provides fast insertions and deletions from both ends, making it much more suitable than lists for queue-based applications.

Whether you’re building Selenium automation frameworks, processing API requests, managing browser history, or scheduling tasks, deque offers a clean and high-performance solution.

Mastering deque is an important step toward understanding Python’s advanced collection types and writing more efficient applications.


Frequently Asked Questions (FAQs)

What does deque stand for?

Double-Ended Queue.


Which module contains deque?

from collections import deque

How do I add an item to the left side?

deque.appendleft(item)

How do I remove an item from the left side?

deque.popleft()

Can deque be used as a queue?

Yes.

It is one of the best data structures for implementing queues in Python.


Key Takeaways

  • deque stands for Double-Ended Queue.

  • It is part of Python’s collections module.

  • Supports fast insertion and deletion from both ends.

  • append() adds to the right.

  • appendleft() adds to the left.

  • pop() removes from the right.

  • popleft() removes from the left.

  • Ideal for queues and stacks.

  • Supports fixed-size collections using maxlen.

  • Commonly used in Selenium and API automation frameworks for task management and processing queues.