Search code examples
pythonarraysslicedeque

Colon Operator with Deques (in Python)


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?


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]