{"record":{"id":"4ccb381d2c622502","repo":"TheAlgorithms/Python","slug":"list-index-out-of-range-4ccb38","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_min.py","lineNumber":72,"sourceCode":"    Traceback (most recent call last):\n        ...\n    IndexError: list index out of range\n    >>> find_min_recursive(nums, -len(nums), -1) == min(nums)\n    True\n    >>> find_min_recursive(nums, -len(nums) - 1, -1) == min(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_min_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_min = find_min_recursive(nums, left, mid)  # find min in range[left, mid]\n    right_min = find_min_recursive(\n        nums, mid + 1, right\n    )  # find min in range[mid + 1, right]\n\n    return left_min if left_min <= right_min else right_min\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod(verbose=True)\n","sourceCodeStart":54,"sourceCodeEnd":88,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/find_min.py#L54-L88","documentation":"Raised by find_min_recursive() in maths/find_min.py when left or right falls outside -len(nums) <= idx < len(nums). The recursion treats right as an inclusive element index (base case left == right returns nums[left]), so the slice-style call right = len(nums) is out of range and triggers IndexError('list index out of range'); negative indices are allowed down to -len(nums) but not one further.","triggerScenarios":"Calling find_min_recursive(nums, 0, len(nums)) with right == len(nums) (shown failing in its doctest), or find_min_recursive(nums, -len(nums) - 1, -1), left >= len(nums), or right < -len(nums). Correct calls use inclusive bounds: (0, len(nums) - 1) or (-len(nums), -1).","commonSituations":"Half-open [start, stop) bounds from slicing/range habits passed to an inclusive-bounds API — the dominant cause; copy-pasting bounds between helpers with different conventions; index arithmetic like right = left + size without a final -1.","solutions":["Pass inclusive bounds: find_min_recursive(nums, 0, len(nums) - 1) or (nums, -len(nums), -1).","Subtract 1 from any exclusive stop before passing it as right.","Add a thin wrapper converting [lo, hi) to (lo, hi - 1) if your codebase standardizes on slice semantics."],"exampleFix":"# before\nfind_min_recursive(nums, 0, len(nums))    # IndexError: right == len(nums)\n\n# after\nfind_min_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)}')\nlo = find_min_recursive(nums, left, right)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Right is inclusive — pass len(nums) - 1, not len(nums).","Convert half-open [lo, hi) bounds with hi - 1 before calling.","Standardize one bounds convention across your codebase and wrap helpers that differ."],"tags":["math","min","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"}