A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.
Questions tagged [deque]
993 questions
19
votes
2 answers
Add to a deque being iterated in Python?
I have a deque in Python that I'm iterating over. Sometimes the deque changes while I'm interating which produces a RuntimeError: deque mutated during iteration.
If this were a Python list instead of a deque, I would just iterate over a copy of the…

Brian Madden
- 951
- 1
- 8
- 21
19
votes
4 answers
dumping queue into list/array in python
I am running a number of threads and collecting there result on a queue. I would like to dump it into array or list so that I can do indexing and retrieve those results. Each of the elements in the queue is a array of dimension n. I would like to…

thetna
- 6,903
- 26
- 79
- 113
17
votes
1 answer
time complexity of random access in deque in Python
I am wondering about the time complexity of the get operation of deque in Python.
I know that it is implemented as a doubly link in Python. Does that mean that its time complexity is O(n)?

DSKim
- 575
- 1
- 6
- 16
16
votes
1 answer
Why are deque's pop_front() and pop_back() not noexcept?
Is there any reason that std::deque's pop_front() and pop_back() are not noexcept in C++11 and higher or was that just forgotten?

Benjamin Buch
- 4,752
- 7
- 28
- 51
16
votes
1 answer
deque remove item by index
Is there any way to remove an item in deque by index?
dq = deque(['a','b','c'])
dq.removeByIndex(1)
#output deque(['b', 'c'])
I only see remove by value in the doc. Also, I know I can just pop it i times and then put it back, but it doesn't look…

Denly
- 899
- 1
- 10
- 21
16
votes
1 answer
How can I get the first value of a deque without deleting it?
I use Python 3.6.1 and I use deque() from collections quite often due to its convenience. This time, I need to get the first value of a deque and wonder if it's possible.
The problem is that I can do that using .popleft(), but it ends up deleting…

maynull
- 1,936
- 4
- 26
- 46
15
votes
3 answers
Confusion on iterators invalidation in deque
I'm bit confused regarding iterator invalidation in deque.
(In the context of this question)
Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference,
By Nicolai M. Josuttis
Any insertion or deletion of elements
…

aJ.
- 34,624
- 22
- 86
- 128
15
votes
1 answer
C++ std::deque implementation: why not use circular buffer?
I did some search on the implementation of deque. According to this post, deque uses vector of vectors. I know pushing at the begin and end should be both in constant time, and also random access is required. I think circular buffer meets all these…

sunnior
- 151
- 1
- 1
- 4
14
votes
4 answers
Why is python deque initialized using the last maxlen items in an iterable?
I was learning how to use deque. Here's what I did:
>>> d = deque([1,2,3,4,5,6], maxlen=3)
I expected that d would contain [1,2,3]. But instead I got:
>>> d
deque([4, 5, 6], maxlen=3)
Isn't this counterintuitive?

Haiyang
- 1,489
- 4
- 15
- 19
14
votes
4 answers
dequeueReusableCellWithReuseIdentifier crash 'could not dequeue a view of kind" UICollectionElementKindCell'
I am getting the following crash:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the…

Ken Roy
- 915
- 1
- 10
- 15
13
votes
4 answers
How to implement deque data structure in javascript?
I'm Learning data structure with javascript
and my focus now on how to implement deque?
Edite: from comments below I get useful directions on how to implement deque based array. Is there a direction how to implement deque based object using class…

Ayman Morsy
- 1,279
- 1
- 10
- 28
13
votes
4 answers
Java equivalent of std::deque
I'm a relatively new Java programmer coming from C++/STL, and am looking for a class with these characteristics (which the C++ std::deque has, as I understand it):
O(1) performance for insertion/removal at the beginning/end
O(1) performance for…

Jason S
- 184,598
- 164
- 608
- 970
13
votes
1 answer
Creating an empty deque in Python with a max length?
I'm looking at the documentation for a Python deque, and it looks like the constructor is deque([iterable[, maxlen]]). Is there no way to make an empty deque (that is, without specifying the iterable) with a max length?

itsmichaelwang
- 2,282
- 4
- 16
- 25
12
votes
2 answers
Deque random acces O(n) in python while O(1) in C++, why?
C++ deque:
Random access - constant O(1)
Python deque:
Indexed access is O(1) at both ends but slows to O(n) in the middle.
If I'm not missing anything, everything else is equally fast for deques in python and in C++, at least complexity-wise.…

Typical User
- 153
- 8
12
votes
3 answers
Is using std::deque or std::priority_queue thread-safe?
Possible Duplicates:
Is the C++ STL std::set thread-safe?
Thread safety for STL queue
I'm guessing it isn't, I just want to make sure.
meaning 2 threads using the same std::deque using std::deque::push_back or push_front at the same time.
Same…

grich
- 463
- 3
- 6
- 14