{"record":{"id":"efd76c952e65c65d","repo":"TheAlgorithms/Python","slug":"list-index-out-of-range-efd76c","errorCode":null,"errorMessage":"List index out of range.","messagePattern":"List index out of range\\.","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"data_structures/linked_list/singly_linked_list.py","lineNumber":313,"sourceCode":"        >>> linked_list.insert_tail(\"second\")\n        >>> linked_list.insert_tail(\"third\")\n        >>> linked_list\n        first -> second -> third\n        >>> linked_list.delete_nth(1) # delete middle\n        'second'\n        >>> linked_list\n        first -> third\n        >>> linked_list.delete_nth(5) # this raises error\n        Traceback (most recent call last):\n            ...\n        IndexError: List index out of range.\n        >>> linked_list.delete_nth(-1) # this also raises error\n        Traceback (most recent call last):\n            ...\n        IndexError: List index out of range.\n        \"\"\"\n        if not 0 <= index <= len(self) - 1:  # test if index is valid\n            raise IndexError(\"List index out of range.\")\n        delete_node = self.head  # default first node\n        if index == 0:\n            self.head = self.head.next_node\n        else:\n            temp = self.head\n            for _ in range(index - 1):\n                temp = temp.next_node\n            delete_node = temp.next_node\n            temp.next_node = temp.next_node.next_node\n        return delete_node.data\n\n    def is_empty(self) -> bool:\n        \"\"\"\n        Check if linked list is empty.\n        >>> linked_list = LinkedList()\n        >>> linked_list.is_empty()\n        True\n        >>> linked_list.insert_head(\"first\")","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/linked_list/singly_linked_list.py#L295-L331","documentation":"LinkedList.delete_nth raises IndexError('List index out of range.') — capital 'L' and trailing period — when index is outside [0, len-1]. Validation happens before any pointer rewiring so the list is unchanged on failure.","triggerScenarios":"delete_nth(len(linked_list)); delete_nth(-1) (both shown in the doctests); delete_nth(0) on an empty list.","commonSituations":"Reusing an insert bounds check (<= len) for delete; deleting in a loop with a cached stale length; parsers that pass user-entered 1-based positions unchecked.","solutions":["Check 0 <= index <= len(linked_list) - 1 before deleting.","In deletion loops, iterate 'while len(linked_list) > target:' or recompute length each pass.","Match the exact message style ('List index...') if tests assert on it; catch IndexError otherwise."],"exampleFix":"# before\nlinked_list.delete_nth(len(linked_list))  # IndexError\n# after\nn = len(linked_list)\nif 0 <= idx <= n - 1:\n    linked_list.delete_nth(idx)","handlingStrategy":"validation","validationCode":"def safe_delete_nth(ll, index):\n    if 0 <= index <= len(ll) - 1:\n        return ll.delete_nth(index)\n    return None","typeGuard":null,"tryCatchPattern":"try:\n    value = ll.delete_nth(i)\nexcept IndexError:\n    value = None","preventionTips":["Valid delete range is 0..len-1; len itself is invalid.","Recompute len() inside delete loops.","Exact message starts with capital 'List' — match it in doctests."],"tags":["linked-list","index-error","delete","bounds-check"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}