How would you extract items 3..6 efficiently, elegantly and pythonically from the following deque
without altering it:
from collections import deque
q = deque('',maxlen=10)
for i in range(10,20):
q.append(i)
the slice notation doesn't seem to work with deque
...
I would prefer this, it's shorter so easier to read:
For example:
This should be more efficient the the other solutions posted so far. Proof?
I'd add this as a new answer, to provide better formatting.
For simplicity, Shawn's answer is perfect, but if you often need to get a slice from
dequeue
, you might prefer to subclass it and add a__getslice__
method.This won't support setting a new slice, but you can implement your own custom
__setslice__
method using the same concept.