Skip to main content

Deque

  • doubly ended queue
  • insert/delete at both ends O(1)
  • Application: Stack & Queue; history of actions

Initialization

from collections import deque
d = deque()

Left operations

d.appendleft(10)
d.popleft()

Right operations

d.append(20)
d.pop()

Others

d.insert(2, 15) # index, value
d.count(10)
d.extend([30,40])

# first 15 then 25 will be added
# new deque -> [25, 15, ...]
d.extendleft([15,25])

d.rotate(2) # clockwise

d.reverse()

d[0] = 5
d[-1]
warning

Slicing is not allowed in deque