Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pop() lots of elements from a deque?

Tags:

I have a deque object what holds a large amount of data. I want to extract, say, 4096 elements from the front of the queue (I'm using it as a kind of FIFO). It seems like there should be way of doing this without having to iterate over 4096 pop requests.

Is this correct/efficient/stupid?

A = arange(100000) B = deque() C = [] # List will do  B.extend(A) # Nice large deque  # extract 4096 elements for i in xrange(4096):    C.append(A.popleft()) 
like image 996
Ross W Avatar asked Feb 29 '12 22:02

Ross W


People also ask

How do you pop elements in a deque python?

The Popping functions on Deque Like appending, there are two different types of pop functions. The pop() method is used to remove and return the right most element from the queue, and popleft() method is used to remove and return left most element from the queue.

How do you pop deque?

pop_back() function is used to pop or remove elements from a deque from the back. The value is removed from the deque from the end, and the container size is decreased by 1.


1 Answers

There is no multi-pop method for deques. You're welcome to submit a feature request to bugs.python.org and I'll consider adding it.

I don't know the details of your use case, but if your data comes in blocks of 4096, consider storing the blocks in tuples or lists and then adding the blocks to the deque:

block = data[:4096] d.append(block) ... someblock = d.popleft() 
like image 124
Raymond Hettinger Avatar answered Oct 25 '22 11:10

Raymond Hettinger