deque

Multiprocessing event-queue not updating

Multiprocessing event-queue not updating Question: So I’m writing a program with an event system. I got a list of events to be handled. One Process is supposed to push to the handler-list new events. This part seems to work as I tried to print out the to-handle-list after pushing one event. It gets longer and …

Total answers: 1

Is there any faster alternative of collection.deque in Python

Is collection.deque the best implementation of constant length list in Python? Question: I want to limit the length of the list in python, when len(list) > limit, the first item will be removed, collection.deque can achieve it, however, will it be slower than: list_A = [2,4,6,8,11] length_limit = 5 while True: # do something to …

Total answers: 1

How to peek front of deque without popping?

How to peek front of deque without popping? Question: 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. Asked By: corinjg || Source Answers: TL;DR: assuming your deque is …

Total answers: 4

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

deque.popleft() and list.pop(0). Is there performance difference? Question: deque.popleft() and list.pop(0) seem to return the same result. Is there any performance difference between them and why? Asked By: Bin || Source Answers: Yes, and it’s considerable if you have a long list or deque. All elements in a list are placed contiguously in memory, so …

Total answers: 3

Colon Operator with Deques (in Python)

Colon Operator with Deques (in Python) Question: I was hoping to use the colon operator with my deque but it didn’t seem to work the same as a list. I was trying something like: myDeque = deque([0,1,2,3,4,5]) myDequeFunction(myDeque[3:]) This is the error I recieved: “TypeError: sequence index must be integer, not ‘slice’” What is the …

Total answers: 3

How to slice a deque?

How to slice a deque? Question: I’ve changed some code that used a list to using a deque. I can no longer slice into it, as I get the error: TypeError: sequence index must be integer, not ‘slice’ Here’s a REPL that shows the problem. >>> import collections >>> d = collections.deque() >>> for i …

Total answers: 2

Use slice notation with collections.deque

Use slice notation with collections.deque Question: 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 work with deque… Asked By: Jonathan Livni || Source Answers: output = [q[i] for …

Total answers: 6

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

How are deques in Python implemented, and when are they worse than lists? Question: 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 …

Total answers: 4

Converting a deque object into list

Converting a deque object into list Question: 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 to …

Total answers: 2

Queue.Queue vs. collections.deque

Queue.Queue vs. collections.deque Question: 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 thread-safe in the documentation. However, the Queue docs also state: collections.deque is …

Total answers: 7