TheAlgorithms/Python · error · ValueError

numbers must be an iterable of integers

Error message

numbers must be an iterable of integers

What it means

Raised by max_product_subarray(numbers) when numbers is not a list/tuple or contains any non-int element. Empty input is special-cased earlier to return 0, so this ValueError means you passed a non-sequence (e.g. an int or string) or a sequence with float/string/None elements. The isinstance((list, tuple)) check also rejects generators and numpy arrays even if their elements are ints.

Source

Thrown at dynamic_programming/max_product_subarray.py:39

    0
    >>> max_product_subarray(None)
    0
    >>> max_product_subarray([2, 3, -2, 4.5, -1])
    Traceback (most recent call last):
        ...
    ValueError: numbers must be an iterable of integers
    >>> max_product_subarray("ABC")
    Traceback (most recent call last):
        ...
    ValueError: numbers must be an iterable of integers
    """
    if not numbers:
        return 0

    if not isinstance(numbers, (list, tuple)) or not all(
        isinstance(number, int) for number in numbers
    ):
        raise ValueError("numbers must be an iterable of integers")

    max_till_now = min_till_now = max_prod = numbers[0]

    for i in range(1, len(numbers)):
        # update the maximum and minimum subarray products
        number = numbers[i]
        if number < 0:
            max_till_now, min_till_now = min_till_now, max_till_now
        max_till_now = max(number, max_till_now * number)
        min_till_now = min(number, min_till_now * number)

        # update the maximum product found till now
        max_prod = max(max_prod, max_till_now)

    return max_prod

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass a plain list of ints: max_product_subarray([int(x) for x in numbers]).
  2. Convert numpy arrays with array.tolist().
  3. Materialize generators with list(...) before the call.

Example fix

# before
best = max_product_subarray(arr)  # numpy array -> ValueError

# after
best = max_product_subarray(arr.tolist())
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(numbers, (list, tuple)):
    numbers = list(numbers)
numbers = [int(n) for n in numbers]
best = max_product_subarray(numbers)

Type guard

def is_int_sequence(values: object) -> bool:
    return isinstance(values, (list, tuple)) and all(
        isinstance(v, int) and not isinstance(v, bool) for v in values
    )

Try / catch

try:
    best = max_product_subarray(numbers)
except ValueError as exc:
    if 'iterable of integers' in str(exc):
        best = max_product_subarray([int(n) for n in list(numbers)])
    else:
        raise

Prevention

When it happens

Trigger: max_product_subarray('ABC') as in the doctest; max_product_subarray([1, 2.5]) with a float element; max_product_subarray(numpy_array) or max_product_subarray(x for x in data) — both fail the list/tuple check.

Common situations: Numeric data parsed as floats from JSON/CSV ('2.5', '3.0'); numpy arrays from ML pipelines; generator expressions chained from upstream transformations.

Related errors


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