TheAlgorithms/Python · error · IndexError
list index out of range
Error message
list index out of range
What it means
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.
Source
Thrown at maths/find_max.py:69
Traceback (most recent call last):
...
IndexError: list index out of range
>>> find_max_recursive(nums, -len(nums), -1) == max(nums)
True
>>> find_max_recursive(nums, -len(nums) - 1, -1) == max(nums)
Traceback (most recent call last):
...
IndexError: list index out of range
"""
if len(nums) == 0:
raise ValueError("find_max_recursive() arg is an empty sequence")
if (
left >= len(nums)
or left < -len(nums)
or right >= len(nums)
or right < -len(nums)
):
raise IndexError("list index out of range")
if left == right:
return nums[left]
mid = (left + right) >> 1 # the middle
left_max = find_max_recursive(nums, left, mid) # find max in range[left, mid]
right_max = find_max_recursive(
nums, mid + 1, right
) # find max in range[mid + 1, right]
return left_max if left_max >= right_max else right_max
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
View on GitHub (pinned to f5988cc097)
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.
Example fix
# before find_max_recursive(nums, 0, len(nums)) # IndexError: right == len(nums) # after find_max_recursive(nums, 0, len(nums) - 1) # inclusive bounds
Defensive patterns
Strategy: validation
Validate before calling
if not (-len(nums) <= left < len(nums) and -len(nums) <= right < len(nums) and left <= right):
raise IndexError(f'bounds ({left}, {right}) invalid for length {len(nums)}')
m = find_max_recursive(nums, left, right) Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- list index out of range
- find_max_recursive() arg is an empty sequence
- Number should not be negative.
- find_max_iterative() arg is an empty sequence
- find_min_recursive() arg is an empty sequence
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/96925ea3c05ad61c.
Report an issue: GitHub.