Deque
Python
Data Structures
Algorithms
Programming

How to check/find if an item is in a DEQUE

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

If you are using Python's collections.deque, the easiest way to check whether an item exists is the in operator. A deque supports iteration, so membership testing works naturally. The important detail is performance: unlike a set or dictionary, deque membership is a linear scan, not constant-time lookup.

The Simple Answer: Use in

Python deque objects support membership testing directly.

python
1from collections import deque
2
3items = deque([10, 20, 30, 40])
4
5print(20 in items)
6print(99 in items)

This is the most idiomatic solution for a yes-or-no existence check.

Under the hood, Python scans through the deque until it finds a match or reaches the end.

If You Need the Position, Convert or Enumerate

A deque does not provide a list-style .index() method in the way many developers expect to use a sequence container. If you need the position of a matching element, enumerate it manually.

python
1from collections import deque
2
3items = deque(["a", "b", "c"])
4
5for index, value in enumerate(items):
6    if value == "b":
7        print(index)
8        break

That works, but it is still an O(n) scan.

If positional access is central to the algorithm, a list may be a better data structure than a deque.

Counting Matches

If you want to know whether an item occurs and how many times, count is available.

python
1from collections import deque
2
3items = deque([1, 2, 1, 3, 1])
4print(items.count(1))

This is also a linear scan, but it is useful when duplicates matter.

Why Membership Is Not Fast Like a Set

A deque is optimized for appending and popping from both ends. It is not designed for fast arbitrary lookup.

That means these are good deque operations:

  • 'append'
  • 'appendleft'
  • 'pop'
  • 'popleft'

But membership tests still require traversal.

If your code repeatedly asks "is this value present?" and rarely uses the double-ended queue behavior, a set may be the more appropriate structure.

python
values = {10, 20, 30, 40}
print(20 in values)

That is usually much faster for repeated membership checks.

A Practical Example

Suppose you are doing breadth-first traversal and keep a deque of nodes to visit.

python
1from collections import deque
2
3queue = deque(["A", "B", "C"])
4
5if "B" in queue:
6    print("already queued")

This is fine for small queues or occasional checks. But if the queue gets large and membership checks happen often, a paired set is usually better.

python
1from collections import deque
2
3queue = deque(["A", "B", "C"])
4queued = set(queue)
5
6candidate = "D"
7if candidate not in queued:
8    queue.append(candidate)
9    queued.add(candidate)

Now:

  • the deque preserves order and cheap pops from the left
  • the set provides fast membership checks

That combination is common in graph algorithms.

When a Deque Is Still the Right Tool

A deque is still the right structure when the primary operations are queue-like or stack-like behavior at both ends.

Use it when you need:

  • fast FIFO processing with popleft
  • occasional membership checks only
  • bounded history windows with maxlen

Do not replace it with a set unless ordering and left-pop behavior are no longer important.

Common Pitfalls

  • Assuming deque membership is constant time like a set. It is a linear scan.
  • Choosing a deque when the workload is really dominated by lookups instead of end operations.
  • Converting the deque to a list repeatedly just to test membership. item in deque already works.
  • Forgetting that a separate set may be needed when queue order and fast membership both matter.
  • Using the wrong data structure because the name "deque" sounds more general-purpose than it really is.

Summary

  • In Python, check whether a value is in a deque with item in dq.
  • Membership testing works, but it is O(n) rather than constant time.
  • Use count if you need the number of matches.
  • Enumerate manually if you need a position.
  • Pair a deque with a set when you need both ordered queue behavior and fast membership checks.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.