TheAlgorithms/Python · error · ValueError

find_min_recursive() arg is an empty sequence

Error message

find_min_recursive() arg is an empty sequence

What it means

Raised by find_min_recursive() in maths/find_min.py when nums is an empty list. The divide-and-conquer minimum recurses down to single-element subranges indexed by left/right; with zero elements there is no base case, so it raises ValueError before any index handling.

Source

Thrown at maths/find_min.py:65

    >>> find_min_recursive(nums, 0, len(nums) - 1) == min(nums)
    True
    >>> find_min_recursive([], 0, 0)
    Traceback (most recent call last):
        ...
    ValueError: find_min_recursive() arg is an empty sequence
    >>> find_min_recursive(nums, 0, len(nums)) == min(nums)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    >>> find_min_recursive(nums, -len(nums), -1) == min(nums)
    True
    >>> find_min_recursive(nums, -len(nums) - 1, -1) == min(nums)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    """
    if len(nums) == 0:
        raise ValueError("find_min_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_min = find_min_recursive(nums, left, mid)  # find min in range[left, mid]
    right_min = find_min_recursive(
        nums, mid + 1, right
    )  # find min in range[mid + 1, right]

    return left_min if left_min <= right_min else right_min

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard for emptiness at the call site and choose a default or explicit error.
  2. Check size where data is loaded or filtered, not deep in the aggregation loop.
  3. Use find_min_iterative or built-in min if divide-and-conquer is not required — same empty-input contract, simpler bounds.

Example fix

# before
lo = find_min_recursive(values, 0, len(values) - 1)  # values == [] -> ValueError

# after
if values:
    lo = find_min_recursive(values, 0, len(values) - 1)
else:
    lo = None
Defensive patterns

Strategy: validation

Validate before calling

if not nums:
    raise ValueError('cannot compute min of empty dataset')
lo = find_min_recursive(nums, 0, len(nums) - 1)

Prevention

When it happens

Trigger: Calling find_min_recursive([], left, right) with any bounds. The `if len(nums) == 0` check fires first; non-empty input with bad bounds instead raises IndexError (see error 559).

Common situations: Empty filtered datasets or empty batches reaching the recursive helper; pipelines that assume at least one record after grouping; tests probing edge cases.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/a1b22de5410070fc. Report an issue: GitHub.