deque rotate / reverse

rotate(1) moves the last item to the front; rotate(-1) moves the first item to the end. n larger than the length wraps around. reverse() flips the order without building a new deque.

deque methodsPython 2.4+ (reverse 3.2+)Live demo
Common call
d.rotate(1) · d.rotate(-1) · d.reverse()
Returns
None
Replaces
lst[-n:] + lst[:-n]
Watch out
positive n rotates RIGHT (last item to the front)
deque.rotate(nn — Steps to the right; negative for left. Equivalent to n pops from one end appended to the other.type: int · default: 1=1)
→ None

Demo

Live evaluation
Positive n: right. Negative n: left.
Try:
Inputs
itemslist[str]items
nintsteps
Code
from collections import deque
d = deque(['a', 'b', 'c', 'd', 'e'])
d.rotate(1)
d
Result
deque(['e', 'a', 'b', 'c', 'd'])

On a five-item deque rotate(7) gives the same result as rotate(2): the steps wrap around the length. Rotating an empty deque is allowed and does nothing.

Parameters

NameTypeRequiredDescription
nintno (1)Steps to the right; negative for left. Equivalent to n pops from one end appended to the other.

Return value

None — Both change the deque in place.

Common patterns

Round-robin scheduling
Serve the front item, then rotate it to the back.
from collections import deque
workers = deque(names)
current = workers[0]
workers.rotate(-1)
Delete the i-th item (docs recipe)
Rotate it to the front, popleft, rotate back.
from collections import deque
d.rotate(-i)
d.popleft()
d.rotate(i)
Reversed copy instead of in place
reversed() gives an iterator and leaves d alone.
from collections import deque
backwards = deque(reversed(d))

Examples

1. Rotate right by one
from collections import deque d = deque([1, 2, 3, 4]) d.rotate() d
Returns
deque([4, 1, 2, 3])
2. Rotate left by one
from collections import deque d = deque([1, 2, 3, 4]) d.rotate(-1) d
Returns
deque([2, 3, 4, 1])
3. n wraps around
from collections import deque d = deque([1, 2, 3]) d.rotate(4) d
Returns
deque([3, 1, 2])
4. reverse in place
from collections import deque d = deque('abc') d.reverse() d
Returns
deque(['c', 'b', 'a'])
5. Rotation keeps maxlen
from collections import deque d = deque([1, 2, 3], maxlen=3) d.rotate(1) d
Returns
deque([3, 1, 2], maxlen=3)
6. n must be an int
from collections import deque deque([1, 2]).rotate(1.5)
Returns
TypeError: 'float' object cannot be interpreted as an integer

Pitfalls

1. Rotating the wrong way
Positive n moves items to the RIGHT (the end wraps to the front). To bring the next item to the front, rotate by -1.
rotate(1)
from collections import deque
d = deque(['a', 'b', 'c'])
d.rotate(1)
d[0]
'c'
rotate(-1)
from collections import deque
d = deque(['a', 'b', 'c'])
d.rotate(-1)
d[0]
'b'
2. Using the return value
rotate and reverse return None, like list.reverse.
d = d.rotate()
from collections import deque
d = deque([1, 2])
d = d.rotate()
print(d)
None
call, keep d
from collections import deque
d = deque([1, 2])
d.rotate()
d
deque([2, 1])

When to use

Use it
  • Round-robin and circular-buffer logic
  • Shifting a sequence without building new lists
Reach for something else
  • A rotated copy of a list you will index heavily → slicing lst[-n:] + lst[:-n]

Notes

CPython impl
_deque_rotate in Modules/_collectionsmodule.c first maps n into the range -len/2 … len/2 (so it never moves more than half the items), then shifts them between blocks
Versions
rotate since 2.4; reverse added in 3.2 (docs.python.org)

FAQ

Put it in a deque and call rotate(n): positive n rotates right, negative left. For a plain list, lst[-n:] + lst[:-n] builds a rotated copy.