TheAlgorithms/Python · error · UnderFlowError
The queue is empty
Error message
The queue is empty
What it means
Raised by ElementPriorityQueue.dequeue() (data_structures/queues/priority_queue_using_list.py:167) when self.queue is empty. It uses the module's custom UnderFlowError, mirroring FixedPriorityQueue's underflow behavior. The normal path pops the minimum-valued element (value = priority).
Source
Thrown at data_structures/queues/priority_queue_using_list.py:167
def __init__(self):
self.queue = []
def enqueue(self, data: int) -> None:
"""
This function enters the element into the queue
If the queue is full an Exception is raised saying Over Flow!
"""
if len(self.queue) == 100:
raise OverFlowError("Maximum queue size is 100")
self.queue.append(data)
def dequeue(self) -> int:
"""
Return the highest priority element in FIFO order.
If the queue is empty then an under flow exception is raised.
"""
if not self.queue:
raise UnderFlowError("The queue is empty")
else:
data = min(self.queue)
self.queue.remove(data)
return data
def __str__(self) -> str:
"""
Prints all the elements within the Element Priority Queue
"""
return str(self.queue)
def fixed_priority_queue():
fpq = FixedPriorityQueue()
fpq.enqueue(0, 10)
fpq.enqueue(1, 70)
fpq.enqueue(0, 100)
fpq.enqueue(2, 1)View on GitHub (pinned to f5988cc097)
Solutions
- Guard with `if pq.queue:` before calling dequeue()
- Catch UnderFlowError imported from data_structures.queues.priority_queue_using_list
- Loop with `while len(pq.queue):` so the loop condition and the dequeue share the same state
Example fix
// before smallest = pq.dequeue() # UnderFlowError on empty # after smallest = pq.dequeue() if pq.queue else None
Defensive patterns
Strategy: validation
Validate before calling
item = pq.dequeue() if pq.queue else None
Try / catch
from data_structures.queues.priority_queue_using_list import UnderFlowError
try:
item = pq.dequeue()
except UnderFlowError:
item = None Prevention
- Use `while pq.queue:` as the drain-loop condition
- Do not catch bare Exception expecting queue.Empty — this module uses UnderFlowError
When it happens
Trigger: dequeue() on a new ElementPriorityQueue, or after the 100th... after all enqueued elements have been dequeued (list is empty).
Common situations: Drain loops (while True: pq.dequeue()), or size checks that race with another consumer of the same queue object.
Related errors
- All queues are empty
- Maximum queue size is 100
- Valid priorities are 0, 1, and 2
- UNDERFLOW
- dequeue from empty queue
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/dab6fa3e328b8c4a.
Report an issue: GitHub.