deque index / count / insert / remove
They behave like their list namesakes, with deque-specific errors: "x is not in deque" from index and remove, and insert refuses to grow a full bounded deque.
Common call
d.index('b') · d.count('b') · d.insert(1, 'x') · d.remove('b')
Returns
int · int · None · None
Replaces
converting to a list for one lookup
Watch out
insert on a full bounded deque raises IndexError
deque.index(x[, start[, stop]])
→ int | None
Demo
Live evaluation
count never fails; index raises ValueError when the value is absent.
Try:
Inputs
itemslist[str]items
xstrvalue to find
Code
from collections import deque d = deque(['a', 'b', 'c', 'b']) (d.count('b'), d.index('b'))
Result
(2, 1)
In "absent" count() returns 0 first, then index() raises ValueError: 'z' is not in deque — remove() raises the same message. Inserting at position 99 simply appends, but inserting into a deque that already holds maxlen items is an IndexError.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| x | object | yes | The value to find, count or insert (compared with ==). |
| start | int | no (0) | index only: first position to search; negative counts from the end. |
| stop | int | no (len(d)) | index only: search stops before this position. |
| i | int | yes | insert only: position; out-of-range values clamp to the ends like list.insert. |
Return value
int | None — index → position, count → number of matches; insert and remove return None.
Common patterns
Find with a default
Test membership first to avoid the ValueError.
pos = d.index(x) if x in d else -1
Remove if present
remove raises when the value is missing.
if x in d: d.remove(x)
Search a range
start/stop limit the scan, like list.index.
second = d.index(x, d.index(x) + 1)
Examples
1. index finds the first match
from collections import deque
deque('abcb').index('b')
Returns
12. index with a start
from collections import deque
deque('abcb').index('b', 2)
Returns
33. count
from collections import deque
deque('banana').count('a')
Returns
34. insert in the middle
from collections import deque
d = deque('ac')
d.insert(1, 'b')
d
Returns
deque(['a', 'b', 'c'])5. remove the first match
from collections import deque
d = deque('abab')
d.remove('b')
d
Returns
deque(['a', 'a', 'b'])6. Missing value
from collections import deque
deque('ab').index('z')
Returns
ValueError: 'z' is not in deque7. insert into a full bounded deque
from collections import deque
deque('ab', maxlen=2).insert(1, 'x')
Returns
IndexError: deque already at its maximum sizePitfalls
1. remove() of a missing value
Like list.remove, it raises ValueError. Check membership first.
remove blindly
from collections import deque d = deque(['a']) d.remove('b')
ValueError: 'b' is not in deque
check with in
from collections import deque d = deque(['a']) if 'b' in d: d.remove('b') d
deque(['a'])
2. Heavy middle access on a deque
index, insert and remove walk the deque. For many position-based edits a list is the better structure; convert once.
many d[i] reads
from collections import deque d = deque(range(5)) [d[i] for i in range(0, 5, 2)]
[0, 2, 4]
slice a list
from collections import deque d = deque(range(5)) list(d)[::2]
[0, 2, 4]
When to use
Use it
- Occasional lookups or edits inside a queue
Reach for something else
- Frequent index-based work → list
- Frequent membership tests → a set alongside the deque
Notes
CPython impl
deque_index, deque_count, deque_insert, deque_remove in Modules/_collectionsmodule.c; each is O(n)
Versions
remove since 2.5, count 3.2, index and insert 3.5 (docs.python.org)
Errors
index/remove: ValueError("<repr> is not in deque"); insert on a full bounded deque: IndexError('deque already at its maximum size')
FAQ
Yes, since Python 3.5: d.index(x[, start[, stop]]) returns the first position of x and raises ValueError when it is absent.