deque append / appendleft / pop / popleft
Four methods, two ends. Pick a pair: append + popleft is a FIFO queue, append + pop is a LIFO stack. On a bounded deque, adding at one end silently drops an item from the other.
Common call
q.append(job) · q.popleft()
Returns
None · the leftmost item
Replaces
list.insert(0, x) and list.pop(0), which are O(n)
Watch out
pop on an empty deque: IndexError: pop from an empty deque
deque.append(xx — The item to add (append, appendleft). pop and popleft take no arguments — there is no pop(i).type: object · required)
→ None | item
Demo
Live evaluation
append goes on the right, appendleft on the left.
Try:
Inputs
itemslist[str]starting items
rightstrappend
leftstrappendleft
Code
from collections import deque d = deque(['a', 'b', 'c']) d.append('z') d.appendleft('y') d
Result
deque(['y', 'a', 'b', 'c', 'z'])
With one item, pop() takes it and popleft() then finds the deque empty: IndexError: pop from an empty deque — the same message for both ends. In the maxlen tab, append("z") on the full a, b, c drops "a", then appendleft("y") drops the "z" that was just added.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| x | object | yes | The item to add (append, appendleft). pop and popleft take no arguments — there is no pop(i). |
Return value
None | item — append and appendleft return None; pop and popleft return the removed item.
Common patterns
FIFO queue
append to enqueue, popleft to dequeue.
from collections import deque queue = deque() queue.append(task) while queue: handle(queue.popleft())
Undo stack with a limit
append + pop on a bounded deque forgets the oldest step.
from collections import deque undo = deque(maxlen=50) undo.append(state) previous = undo.pop()
Pop only when non-empty
An empty deque is falsy.
item = d.popleft() if d else None
Examples
1. append and appendleft
from collections import deque
d = deque(['b'])
d.append('c')
d.appendleft('a')
d
Returns
deque(['a', 'b', 'c'])2. pop takes from the right
from collections import deque
deque([1, 2, 3]).pop()
Returns
33. popleft takes from the left
from collections import deque
deque([1, 2, 3]).popleft()
Returns
14. FIFO order
from collections import deque
q = deque()
for job in ['a', 'b', 'c']:
q.append(job)
[q.popleft() for _ in range(3)]
Returns
['a', 'b', 'c']5. Full bounded deque drops the other end
from collections import deque
d = deque([1, 2, 3], maxlen=3)
d.appendleft(0)
d
Returns
deque([0, 1, 2], maxlen=3)6. Empty deque
from collections import deque
deque().popleft()
Returns
IndexError: pop from an empty dequePitfalls
1. pop(0) like a list
deque.pop takes no index. Use popleft() for the left end.
d.pop(0)
from collections import deque deque([1, 2]).pop(0)
TypeError: deque.pop() takes no arguments (1 given)
d.popleft()
from collections import deque deque([1, 2]).popleft()
1
2. Popping without checking
Both pops raise IndexError on an empty deque. Test the deque (it is falsy when empty) or catch the error.
pop blindly
from collections import deque d = deque() d.pop()
IndexError: pop from an empty deque
check first
from collections import deque d = deque() d.pop() if d else 'empty'
'empty'
3. Losing data silently at maxlen
A full bounded deque never raises on append — it discards. Check len(d) == d.maxlen if that matters.
append to full
from collections import deque d = deque(['keep'], maxlen=1) d.append('new') d
deque(['new'], maxlen=1)
check capacity
from collections import deque d = deque(['keep'], maxlen=1) len(d) == d.maxlen
True
When to use
Use it
- Queues (append + popleft) and stacks (append + pop)
- Adding at the front without O(n) shifting
Reach for something else
- Removing by index from the middle → del d[i] works, but a list may suit better
- Blocking hand-off between threads → queue.Queue
Notes
CPython impl
Modules/_collectionsmodule.c — deque_append, deque_appendleft, deque_pop, deque_popleft; all O(1)
Empty
pop and popleft raise IndexError("pop from an empty deque")
maxlen
When full, append discards from the left and appendleft from the right; with maxlen=0 nothing is ever stored
FAQ
d.appendleft(x). It is O(1), unlike list.insert(0, x), which shifts every element.