TheAlgorithms/Python · error · ValueError

find_min_iterative() arg is an empty sequence

Error message

find_min_iterative() arg is an empty sequence

What it means

Raised by find_min_iterative() in maths/find_min.py when nums is an empty sequence. The minimum of zero elements does not exist; the function seeds min_num = nums[0] and would otherwise crash, so it raises ValueError up front — same contract as built-in min().

Source

Thrown at maths/find_min.py:24

    Find Minimum Number in a List
    :param nums: contains elements
    :return: min number in list

    >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
    ...     find_min_iterative(nums) == min(nums)
    True
    True
    True
    True
    >>> find_min_iterative([0, 1, 2, 3, 4, 5, -3, 24, -56])
    -56
    >>> find_min_iterative([])
    Traceback (most recent call last):
        ...
    ValueError: find_min_iterative() arg is an empty sequence
    """
    if len(nums) == 0:
        raise ValueError("find_min_iterative() arg is an empty sequence")
    min_num = nums[0]
    for num in nums:
        min_num = min(min_num, num)
    return min_num


# Divide and Conquer algorithm
def find_min_recursive(nums: list[int | float], left: int, right: int) -> int | float:
    """
    find min value in list
    :param nums: contains elements
    :param left: index of first element
    :param right: index of last element
    :return: min in nums

    >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):
    ...     find_min_recursive(nums, 0, len(nums) - 1) == min(nums)
    True

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check `if not nums:` before calling and supply a default or raise a domain error.
  2. Use built-in min(nums, default=...) if you want empty-input tolerance.
  3. Validate collection size where the data enters your program.

Example fix

# before
low = find_min_iterative(readings)  # readings == [] -> ValueError

# after
low = find_min_iterative(readings) if readings else 0.0
Defensive patterns

Strategy: validation

Validate before calling

if not nums:
    raise ValueError('cannot compute min of empty input')
low = find_min_iterative(nums)

Type guard

def is_nonempty_seq(nums: object) -> bool:
    return hasattr(nums, '__len__') and len(nums) > 0

Prevention

When it happens

Trigger: Calling find_min_iterative([]) (per its doctest). The `if len(nums) == 0` guard raises before nums[0] is accessed.

Common situations: Aggregating over empty query results, empty sensor batches, or config-driven lists that were never populated; iterating paginated APIs where a page comes back empty.

Related errors


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