{"record":{"id":"e6172746c78a38b5","repo":"TheAlgorithms/Python","slug":"dequeue-from-empty-queue","errorCode":null,"errorMessage":"dequeue from empty queue","messagePattern":"dequeue from empty queue","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/queues/linked_queue.py","lineNumber":131,"sourceCode":"            self.rear = node\n\n    def get(self) -> Any:\n        \"\"\"\n        >>> queue = LinkedQueue()\n        >>> queue.get()\n        Traceback (most recent call last):\n            ...\n        IndexError: dequeue from empty queue\n        >>> queue = LinkedQueue()\n        >>> for i in range(1, 6):\n        ...     queue.put(i)\n        >>> for i in range(1, 6):\n        ...     assert queue.get() == i\n        >>> len(queue)\n        0\n        \"\"\"\n        if self.is_empty():\n            raise IndexError(\"dequeue from empty queue\")\n        assert isinstance(self.front, Node)\n        node = self.front\n        self.front = self.front.next\n        if self.front is None:\n            self.rear = None\n        return node.data\n\n    def clear(self) -> None:\n        \"\"\"\n        >>> queue = LinkedQueue()\n        >>> for i in range(1, 6):\n        ...     queue.put(i)\n        >>> queue.clear()\n        >>> len(queue)\n        0\n        >>> str(queue)\n        ''\n        \"\"\"","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/queues/linked_queue.py#L113-L149","documentation":"Raised by LinkedQueue.get() (data_structures/queues/linked_queue.py:131) when the singly-linked queue has no nodes. The class follows Python's queue conventions but signals underflow with a builtin IndexError instead of queue.Empty, matching doctest expectations. It is a normal control-flow guard, not a corruption bug.","triggerScenarios":"Calling get() more times than put() on a LinkedQueue: get() on a fresh queue, or draining 5 items after 5 put() calls and calling get() a 6th time (front and rear are both None).","commonSituations":"Consumer loops that dequeue until failure (while True: queue.get()), producer/consumer code where the consumer outruns the producer, and reusing a queue after clear().","solutions":["Guard every get() with a len(queue) > 0 or not queue.is_empty() check","If the doctests/scripts show the pattern, catch IndexError explicitly around get()","Restructure the loop to consume exactly len(queue) items: while len(queue): item = queue.get()"],"exampleFix":"// before\nwhile True:\n    item = queue.get()  # IndexError on last iteration\n\n# after\nwhile len(queue):\n    item = queue.get()","handlingStrategy":"validation","validationCode":"if queue.is_empty():  # or: if not len(queue):\n    raise LookupError('no items to dequeue')\nitem = queue.get()","typeGuard":null,"tryCatchPattern":"try:\n    item = queue.get()\nexcept IndexError as e:\n    if str(e) != 'dequeue from empty queue':\n        raise\n    item = None","preventionTips":["Drain queues with `while len(queue):` instead of pop-until-exception","Track the count of put() calls in producer/consumer code"],"tags":["queue","data-structures","python","index-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}