{"record":{"id":"44ad782e3f7e1fa3","repo":"TheAlgorithms/Python","slug":"valid-priorities-are-0-1-and-2","errorCode":null,"errorMessage":"Valid priorities are 0, 1, and 2","messagePattern":"Valid priorities are 0, 1, and 2","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/queues/priority_queue_using_list.py","lineNumber":87,"sourceCode":"    def __init__(self):\n        self.queues = [\n            [],\n            [],\n            [],\n        ]\n\n    def enqueue(self, priority: int, data: int) -> None:\n        \"\"\"\n        Add an element to a queue based on its priority.\n        If the priority is invalid ValueError is raised.\n        If the queue is full an OverFlowError is raised.\n        \"\"\"\n        try:\n            if len(self.queues[priority]) >= 100:\n                raise OverflowError(\"Maximum queue size is 100\")\n            self.queues[priority].append(data)\n        except IndexError:\n            raise ValueError(\"Valid priorities are 0, 1, and 2\")\n\n    def dequeue(self) -> int:\n        \"\"\"\n        Return the highest priority element in FIFO order.\n        If the queue is empty then an under flow exception is raised.\n        \"\"\"\n        for queue in self.queues:\n            if queue:\n                return queue.pop(0)\n        raise UnderFlowError(\"All queues are empty\")\n\n    def __str__(self) -> str:\n        return \"\\n\".join(f\"Priority {i}: {q}\" for i, q in enumerate(self.queues))\n\n\nclass ElementPriorityQueue:\n    \"\"\"\n    Element Priority Queue is the same as Fixed Priority Queue except that the value of","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/queues/priority_queue_using_list.py#L69-L105","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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"],"exampleFix":"// before\npq.enqueue(user_priority, data)  # user_priority=4 -> ValueError\n\n# after\nmapped = min(max(user_priority, 0), 2)\npq.enqueue(mapped, data)","handlingStrategy":"type-guard","validationCode":"if priority not in (0, 1, 2):\n    raise ValueError(f'priority {priority} not in 0..2')","typeGuard":"def is_valid_priority(p: int) -> bool:\n    return isinstance(p, int) and not isinstance(p, bool) and 0 <= p <= 2","tryCatchPattern":"try:\n    pq.enqueue(priority, data)\nexcept ValueError:\n    # re-map or log; note -1 silently wraps, validate BEFORE the call\n    priority = min(max(priority, 0), 2)\n    pq.enqueue(priority, data)","preventionTips":["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"],"tags":["queue","priority-queue","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}