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 minimum_subarray_sum(target, numbers) when numbers is not a list/tuple or contains non-int elements. Empty numbers returns 0 earlier, and target==0 present in numbers returns 0 before this check; otherwise the sliding-window algorithm requires an int sequence. Generators, strings, numpy arrays, and float-containing lists all fail.
Source
Thrown at dynamic_programming/minimum_size_subarray_sum.py:49
>>> minimum_subarray_sum(-6, [])
0
>>> minimum_subarray_sum(-6, [3, 4, 5])
1
>>> minimum_subarray_sum(8, None)
0
>>> minimum_subarray_sum(2, "ABC")
Traceback (most recent call last):
...
ValueError: numbers must be an iterable of integers
"""
if not numbers:
return 0
if target == 0 and target in 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")
left = right = curr_sum = 0
min_len = sys.maxsize
while right < len(numbers):
curr_sum += numbers[right]
while curr_sum >= target and left <= right:
min_len = min(min_len, right - left + 1)
curr_sum -= numbers[left]
left += 1
right += 1
return 0 if min_len == sys.maxsize else min_len
View on GitHub (pinned to f5988cc097)
Solutions
- Coerce to a list of ints: minimum_subarray_sum(target, [int(x) for x in numbers]).
- Use list(range(...)) rather than a raw range/generator.
- Convert numpy inputs with .tolist().
Example fix
# before length = minimum_subarray_sum(7, (int(x) for x in raw)) # generator -> ValueError # after length = minimum_subarray_sum(7, [int(x) for x in raw])
Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(numbers, (list, tuple)):
numbers = list(numbers)
numbers = [int(n) for n in numbers]
length = minimum_subarray_sum(target, numbers) Type guard
def is_int_sequence(values: object) -> bool:
return isinstance(values, (list, tuple)) and all(isinstance(v, int) for v in values) Try / catch
try:
length = minimum_subarray_sum(target, numbers)
except ValueError as exc:
if 'iterable of integers' in str(exc):
length = minimum_subarray_sum(target, [int(n) for n in numbers])
else:
raise Prevention
- Convert stdin/web-form numbers with int() before algorithm calls.
- Wrap generators in list() — sliding-window APIs need indexed access.
- Validate element types once at ingestion rather than per algorithm call.
When it happens
Trigger: minimum_subarray_sum(2, 'ABC') as in the doctest; minimum_subarray_sum(7, [1.5, 2, 3]) with a float; passing a numpy array or map object as numbers.
Common situations: Sensor/metrics data arriving as floats; string inputs from stdin or web forms; feeding a range or generator directly instead of a list.
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/d8a657e498e0484a.
Report an issue: GitHub.