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
- Pass a plain list of ints: max_product_subarray([int(x) for x in numbers]).
- Convert numpy arrays with array.tolist().
- 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
- Normalize numeric pipelines to list[int] before calling pure-Python DP helpers.
- Use .tolist() when bridging from numpy.
- Coerce float parses to int only after confirming values are integral.
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
- iterations must be defined as integers
- All weights must be integers but got weight of type {type(wt
- numbers must be an iterable of integers
- The parameter days should be a list of integers
- 'float' object cannot be interpreted as an integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/b0e676890edeb991.
Report an issue: GitHub.