TheAlgorithms/Python · error · ValueError
Valid priorities are 0, 1, and 2
Error message
Valid priorities are 0, 1, and 2
What it means
Raised by FixedPriorityQueue.enqueue() (data_structures/queues/priority_queue_using_list.py:87) when the priority argument indexes outside the self.queues list of exactly three buckets. The implementation catches the resulting IndexError and re-raises it as ValueError with this clearer message, documenting that only priorities 0, 1, 2 exist.
Source
Thrown at data_structures/queues/priority_queue_using_list.py:87
def __init__(self):
self.queues = [
[],
[],
[],
]
def enqueue(self, priority: int, data: int) -> None:
"""
Add an element to a queue based on its priority.
If the priority is invalid ValueError is raised.
If the queue is full an OverFlowError is raised.
"""
try:
if len(self.queues[priority]) >= 100:
raise OverflowError("Maximum queue size is 100")
self.queues[priority].append(data)
except IndexError:
raise ValueError("Valid priorities are 0, 1, and 2")
def dequeue(self) -> int:
"""
Return the highest priority element in FIFO order.
If the queue is empty then an under flow exception is raised.
"""
for queue in self.queues:
if queue:
return queue.pop(0)
raise UnderFlowError("All queues are empty")
def __str__(self) -> str:
return "\n".join(f"Priority {i}: {q}" for i, q in enumerate(self.queues))
class ElementPriorityQueue:
"""
Element Priority Queue is the same as Fixed Priority Queue except that the value ofView on GitHub (pinned to f5988cc097)
Solutions
- Clamp or map your priorities to 0, 1, 2 before calling enqueue
- Validate explicitly: if priority not in (0, 1, 2): raise ... with your own message
- If you need arbitrary priority levels, switch to heapq-based priority queue instead of this fixed 3-bucket demo class
Example fix
// before pq.enqueue(user_priority, data) # user_priority=4 -> ValueError # after mapped = min(max(user_priority, 0), 2) pq.enqueue(mapped, data)
Defensive patterns
Strategy: type-guard
Validate before calling
if priority not in (0, 1, 2):
raise ValueError(f'priority {priority} not in 0..2') Type guard
def is_valid_priority(p: int) -> bool:
return isinstance(p, int) and not isinstance(p, bool) and 0 <= p <= 2 Try / catch
try:
pq.enqueue(priority, data)
except ValueError:
# re-map or log; note -1 silently wraps, validate BEFORE the call
priority = min(max(priority, 0), 2)
pq.enqueue(priority, data) Prevention
- Map external priority scales to 0/1/2 at the boundary
- Never use negative priorities with this class — Python negative indexing silently targets another bucket
When it happens
Trigger: enqueue(3, data), enqueue(-4, data), or any priority where self.queues[priority] raises IndexError. Beware: negative indices like -1 do NOT raise IndexError (they index from the end), so priority=-1 silently writes to bucket 2 instead of erroring.
Common situations: Mapping external priority schemes (1-5, 0-10, or 'high'/'low' strings) directly onto this class, off-by-one after converting from a 1-based priority system, or assuming negative priority means 'lowest'.
Related errors
- Maximum queue size is 100
- All queues are empty
- The queue is empty
- number must be positive
- The value of input must be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/44ad782e3f7e1fa3.
Report an issue: GitHub.