from collections import deque items = deque([1, 2]) items.append(3) # deque == [1, 2, 3] items.rotate(1) # The deque is now: [3, 1, 2] items.rotate(-1) # Returns deque to original state: [1, 2, 3] item = items.popleft() # deque == [2, 3]
Here is what the above code is Doing:
1. Create a deque with the numbers 1 and 2.
2. Append the number 3 to the deque.
3. Rotate the deque to the left by one.
4. Rotate the deque to the right by one.
5. Pop and return the leftmost item from the deque.