Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to slice a deque? [duplicate]

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 in range(3): ...     d.append(i) ... >>> d deque([0, 1, 2]) >>> d[2:] Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: sequence index must be integer, not 'slice' 

So, is there a workaround to support slicing into deques in Python?

like image 883
Drew Noakes Avatar asked Apr 03 '12 23:04

Drew Noakes


People also ask

Can you slice deque?

Deques support indexing, but interestingly, they don't support slicing.

Can you slice deque Python?

makes sense to implement slicing in the deque class. The deque iterator has the same internal access to the linked list. You can do it just as efficiently by rotating the deque (unless you need to use the deque concurrently).

Can you index a deque?

To directly answer your question, you index a deque with an iterable of indices the same as you do a list - [test[i] for i in idx] . But deque random lookup is O(n) (which could matter more for larger deques), and if you want to do NumPy-style indexing into the deque you won't be able to.

How do I use deque as a stack in Python?

Use deque as a stack (LIFO) A stack holds data in a LIFO (Last In, First Out) structure. In a stack, inserting data is called push, and removing data is called pop. To use deque as a stack, use append() as a push and pop() as a pop.


1 Answers

Try itertools.islice().

 deque_slice = collections.deque(itertools.islice(my_deque, 10, 20)) 

Indexing into a deque requires following a linked list from the beginning each time, so the islice() approach, skipping items to get to the start of the slice, will give the best possible performance (better than coding it as an index operation for each element).

You could easily write a deque subclass that does this automagically for you.

class sliceable_deque(collections.deque):     def __getitem__(self, index):         if isinstance(index, slice):             return type(self)(itertools.islice(self, index.start,                                                index.stop, index.step))         return collections.deque.__getitem__(self, index) 

Note that you can't use negative indices or step values with islice. It's possible to code around this, and might be worthwhile to do so if you take the subclass approach. For negative start or stop you can just add the length of the deque; for negative step, you'll need to throw a reversed() in there somewhere. I'll leave that as an exercise. :-)

The performance of retrieving individual items from the deque will be slightly reduced by the if test for the slice. If this is an issue, you can use an EAFP pattern to ameliorate this somewhat -- at the cost of making the slice path slightly less performant due to the need to process the exception:

class sliceable_deque(collections.deque):     def __getitem__(self, index):         try:             return collections.deque.__getitem__(self, index)         except TypeError:             return type(self)(itertools.islice(self, index.start,                                                index.stop, index.step)) 

Of course there's an extra function call in there still, compared to a regular deque, so if you really care about performance, you really want to add a separate slice() method or the like.

like image 96
kindall Avatar answered Sep 25 '22 01:09

kindall