{"record":{"id":"96925ea3c05ad61c","repo":"TheAlgorithms/Python","slug":"list-index-out-of-range-96925e","errorCode":null,"errorMessage":"list index out of range","messagePattern":"list index out of range","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"maths/find_max.py","lineNumber":69,"sourceCode":"    Traceback (most recent call last):\n        ...\n    IndexError: list index out of range\n    >>> find_max_recursive(nums, -len(nums), -1) == max(nums)\n    True\n    >>> find_max_recursive(nums, -len(nums) - 1, -1) == max(nums)\n    Traceback (most recent call last):\n        ...\n    IndexError: list index out of range\n    \"\"\"\n    if len(nums) == 0:\n        raise ValueError(\"find_max_recursive() arg is an empty sequence\")\n    if (\n        left >= len(nums)\n        or left < -len(nums)\n        or right >= len(nums)\n        or right < -len(nums)\n    ):\n        raise IndexError(\"list index out of range\")\n    if left == right:\n        return nums[left]\n    mid = (left + right) >> 1  # the middle\n    left_max = find_max_recursive(nums, left, mid)  # find max in range[left, mid]\n    right_max = find_max_recursive(\n        nums, mid + 1, right\n    )  # find max in range[mid + 1, right]\n\n    return left_max if left_max >= right_max else right_max\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod(verbose=True)\n","sourceCodeStart":51,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/find_max.py#L51-L85","documentation":"Raised by find_max_recursive() in maths/find_max.py when the left or right index argument falls outside the permitted range. Valid indices must satisfy -len(nums) <= idx < len(nums) (Python-style negative indexing allowed, but not >= len(nums)); the guard raises IndexError('list index out of range') — including the common mistake right = len(nums), since the recursion treats right as an inclusive element index, not an exclusive bound.","triggerScenarios":"Calling find_max_recursive(nums, 0, len(nums)) — right equals len(nums), which is out of range (doctest shows this exact failure). Also find_max_recursive(nums, -len(nums) - 1, -1), left >= len(nums), or right < -len(nums). Correct inclusive usage is (0, len(nums) - 1) or (-len(nums), -1).","commonSituations":"Passing slice-style exclusive bounds (0, len(nums)) to an API that expects inclusive bounds — the most frequent cause; porting code from binary-search helpers whose conventions differ; mixing negative and positive indices between calls.","solutions":["Use inclusive bounds: find_max_recursive(nums, 0, len(nums) - 1), or negative form (nums, -len(nums), -1).","Never pass len(nums) as right; subtract 1 first.","Wrap the helper in your own function that converts half-open [lo, hi) bounds to inclusive (lo, hi - 1) if your codebase uses slice semantics."],"exampleFix":"# before\nfind_max_recursive(nums, 0, len(nums))    # IndexError: right == len(nums)\n\n# after\nfind_max_recursive(nums, 0, len(nums) - 1)  # inclusive bounds","handlingStrategy":"validation","validationCode":"if not (-len(nums) <= left < len(nums) and -len(nums) <= right < len(nums) and left <= right):\n    raise IndexError(f'bounds ({left}, {right}) invalid for length {len(nums)}')\nm = find_max_recursive(nums, left, right)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Right is inclusive: always pass len(nums) - 1, never len(nums).","Prefer the canonical call find_max_recursive(nums, 0, len(nums) - 1).","Document inclusive-bounds semantics at every call site you wrap."],"tags":["math","max","recursion","index-error","bounds","inclusive-exclusive"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}