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

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


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()


Where you're using a deque the .popleft() method is really the best method of getting elements off the front. You can index into it, but index performance degrades toward the middle of the deque (as opposed to a list that has quick indexed access, but slow pops). You could get away with this though (saves a few lines of code):

A = arange(100000)B = deque(A)C = [B.popleft() for _i in xrange(4096)]