Questions tagged [deque]

A double-ended queue. Container datatype which is typically supporting efficient insertion and removal from two ends.

993 questions
260
votes
8 answers

Queue.Queue vs. collections.deque

I need a queue which multiple threads can put stuff into, and multiple threads may read from. Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be…
miracle2k
  • 29,597
  • 21
  • 65
  • 64
252
votes
10 answers

Why is ArrayDeque better than LinkedList

I am trying to to understand why Java's ArrayDeque is better than Java's LinkedList as they both implement Deque interface. I hardly see someone using ArrayDeque in their code. If someone sheds more light into how ArrayDeque is implemented, it…
Cruel
  • 2,523
  • 2
  • 15
  • 4
244
votes
8 answers

What really is a deque in STL?

I was looking at STL containers and trying to figure what they really are (i.e. the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would allow insertion and deletion from both ends in…
Zonko
  • 3,365
  • 3
  • 20
  • 29
244
votes
8 answers

Why should I use Deque over Stack?

I need a Stack data structure for my use case. I should be able to push items into the data structure and I only want to retrieve the last item from the Stack. The JavaDoc for Stack says : A more complete and consistent set of LIFO stack operations…
Geek
  • 26,489
  • 43
  • 149
  • 227
135
votes
9 answers

What's the difference between deque and list STL containers?

What is the difference between the two? I mean the methods are all the same. So, for a user, they work identically. Is that correct??
Lazer
  • 90,700
  • 113
  • 281
  • 364
108
votes
4 answers

How are deques in Python implemented, and when are they worse than lists?

I've recently gotten into investigating how various data structures are implemented in Python in order to make my code more efficient. In investigating how lists and deques work, I found that I can get benefits when I want to shift and unshift…
Eli
  • 36,793
  • 40
  • 144
  • 207
93
votes
10 answers

Why would I prefer using vector to deque

Since they are both contiguous memory containers; feature wise, deque has almost everything vector has but more, since it is more efficient to insert in the front. Why whould anyone prefer std::vector to std::deque?
Leon
  • 8,151
  • 11
  • 45
  • 51
85
votes
5 answers

python: deque vs list performance comparison

In python docs I can see that deque is a special collection highly optimized for poping/adding items from left or right sides. E.g. documentation says: Deques are a generalization of stacks and queues (the name is pronounced “deck” and is short…
Oleg Tarasenko
  • 9,324
  • 18
  • 73
  • 102
69
votes
6 answers

Use slice notation with collections.deque

How would you extract items 3..6 efficiently, elegantly and pythonically from the following deque without altering it: from collections import deque q = deque('',maxlen=10) for i in range(10,20): q.append(i) the slice notation doesn't seem to…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
65
votes
3 answers

deque.popleft() and list.pop(0). Is there performance difference?

deque.popleft() and list.pop(0) seem to return the same result. Is there any performance difference between them and why?
Bin
  • 3,645
  • 10
  • 33
  • 57
64
votes
8 answers

Why do we need Deque data structures in the real world?

Can anyone give me an example of situation where a Deque data structure is needed? Note - Please don't explain what a deque is?
user366312
  • 16,949
  • 65
  • 235
  • 452
57
votes
2 answers

Converting a deque object into list

Currently, I fetch "list" data from my storage, "deque" it to work with that data. After processing the fetched data, I have to put them back into the storage. This won't be a problem as long as I am not forced to use Python's standard "list" object…
Julius F
  • 3,434
  • 4
  • 29
  • 44
57
votes
2 answers

Queue vs Dequeue in java

What is the difference between them? I know that A queue is designed to have elements inserted at the end of the queue, and elements removed from the beginning of the queue. Where as Dequeue represents a queue where you can insert and remove…
user6503581
53
votes
5 answers

Why is std::vector so much more popular than std::deque?

Possible Duplicate: Why would I prefer using vector to deque I am curious why is it that std::vector is so much more popular than std::deque. Deque is almost as efficient in lookup, more efficient in insert (without vector::reserve)and allows for…
Karthik T
  • 31,456
  • 5
  • 68
  • 87
51
votes
4 answers

How to peek front of deque without popping?

I want to check a condition against the front of a queue before deciding whether or not to pop. How can I achieve this in python with collections.deque? list(my_deque)[0] seems ugly and poor for performance.
jsstuball
  • 4,104
  • 7
  • 33
  • 63
1
2 3
66 67