{"record":{"id":"ed286cfe4f3e2a6f","repo":"TheAlgorithms/Python","slug":"pop-from-empty-stack","errorCode":null,"errorMessage":"pop from empty stack","messagePattern":"pop from empty stack","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/stacks/stack_with_singly_linked_list.py","lineNumber":126,"sourceCode":"    def pop(self) -> T:\r\n        \"\"\"\r\n        >>> stack = LinkedStack()\r\n        >>> stack.pop()\r\n        Traceback (most recent call last):\r\n            ...\r\n        IndexError: pop from empty stack\r\n        >>> stack.push(\"c\")\r\n        >>> stack.push(\"b\")\r\n        >>> stack.push(\"a\")\r\n        >>> stack.pop() == 'a'\r\n        True\r\n        >>> stack.pop() == 'b'\r\n        True\r\n        >>> stack.pop() == 'c'\r\n        True\r\n        \"\"\"\r\n        if self.is_empty():\r\n            raise IndexError(\"pop from empty stack\")\r\n        assert isinstance(self.top, Node)\r\n        pop_node = self.top\r\n        self.top = self.top.next\r\n        return pop_node.data\r\n\r\n    def peek(self) -> T:\r\n        \"\"\"\r\n        >>> stack = LinkedStack()\r\n        >>> stack.push(\"Java\")\r\n        >>> stack.push(\"C\")\r\n        >>> stack.push(\"Python\")\r\n        >>> stack.peek()\r\n        'Python'\r\n        \"\"\"\r\n        if self.is_empty():\r\n            raise IndexError(\"peek from empty stack\")\r\n\r\n        assert self.top is not None\r","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/stacks/stack_with_singly_linked_list.py#L108-L144","documentation":"Raised by LinkedStack.pop() (data_structures/stacks/stack_with_singly_linked_list.py:126) when the singly-linked stack's top pointer is None. It mirrors CPython's list.pop error message deliberately, but is a manual raise after an is_empty() check. push(), peek(), and len() are the complementary API.","triggerScenarios":"pop() on a newly constructed LinkedStack(), or calling pop() more times than push() (the doctest pops 'a','b','c' after three pushes; a fourth pop raises).","commonSituations":"Delimiter-matching or backtracking algorithms that pop in loops, or state machines that pop on a branch the pushes did not cover.","solutions":["Guard with `if not stack.is_empty():` or `while len(stack):`","Catch IndexError when using pop-until-empty as the loop terminator","Audit algorithm branches so every pop() is paired with a prior push() on that path"],"exampleFix":"// before\nwhile True:\n    node = stack.pop()\n\n# after\nwhile stack:\n    node = stack.pop()","handlingStrategy":"validation","validationCode":"if stack.is_empty():\n    return None\nitem = stack.pop()","typeGuard":null,"tryCatchPattern":"try:\n    item = stack.pop()\nexcept IndexError as e:\n    if str(e) != 'pop from empty stack':\n        raise\n    item = None","preventionTips":["Use `while stack:` / `while len(stack):` as drain-loop conditions","In matching/backtracking algorithms, audit that every branch that pops also pushes"],"tags":["stack","data-structures","index-error","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}