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 best way to do array slicing with deques?

Was it helpful?

Solution

Iterating is probably faster than brute-force methods (note: unproven) due to the nature of a deque.

>>> myDeque = collections.deque([0,1,2,3,4,5])
>>> list(itertools.islice(myDeque, 3, sys.maxint))
[3, 4, 5]

OTHER TIPS

deque objects don't support slicing themselves, but you can make a new deque:

sliced_deque = deque(list(old_deque)[3:])

collections.deque objects don't support slicing. It'd be more straightforward to make a new one.

n_deque = deque(list(d)[3:])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top