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?
Try
itertools.islice().Indexing into a
dequerequires following a linked list from the beginning each time, so theislice()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
dequesubclass that does this automagically for you.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 areversed()in there somewhere. I’ll leave that as an exercise. 🙂The performance of retrieving individual items from the
dequewill be slightly reduced by theiftest 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: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 separateslice()method or the like.