Skip to the workspace
Pebellslearn by building

Python syntax & reference

Find the form you need, compare how it behaves, and open the complete explanation when you need more depth.

Python 3.10+ syntax; individual entries identify newer features. Browser exercises use the configured Python runner.

1 matching syntax entry

Queues and priority queues

Learn in depth →

deque supports both ends; heapq keeps the smallest item at the root of a list-based min-heap.

deque.append; deque.popleft; heappush; heappop

Example

from collections import deque
from heapq import heappush, heappop
q = deque([1, 2]); q.append(3)
heap = []; heappush(heap, 8); heappush(heap, 2)
print(q.popleft(), heappop(heap))

Result

1 2

Watch for: A heap is not a globally sorted list. Repeated heappop operations retrieve items in priority order.

Practise this topic →
Complete explanation index (91 lessons indexed)

deque and heapq

Technical sources and review date

Original Pebells examples. Reviewed 2026-10-28.

Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.