TheAlgorithms/Python · error · ValueError
Input list must be a non empty list
Error message
Input list must be a non empty list
What it means
Raised by is_harmonic_series() in maths/series/harmonic.py when the input is a list with zero elements. An empty series has no reciprocals to compare, so the common-difference test on the reciprocal sequence is impossible and the function rejects the input early with a clear message.
Source
Thrown at maths/series/harmonic.py:43
...
ValueError: Input series is not valid, valid series - [1, 2/3, 2]
>>> is_harmonic_series([])
Traceback (most recent call last):
...
ValueError: Input list must be a non empty list
>>> is_harmonic_series([0])
Traceback (most recent call last):
...
ValueError: Input series cannot have 0 as an element
>>> is_harmonic_series([1,2,0,6])
Traceback (most recent call last):
...
ValueError: Input series cannot have 0 as an element
"""
if not isinstance(series, list):
raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]")
if len(series) == 0:
raise ValueError("Input list must be a non empty list")
if len(series) == 1 and series[0] != 0:
return True
rec_series = []
series_len = len(series)
for i in range(series_len):
if series[i] == 0:
raise ValueError("Input series cannot have 0 as an element")
rec_series.append(1 / series[i])
common_diff = rec_series[1] - rec_series[0]
for index in range(2, series_len):
if rec_series[index] - rec_series[index - 1] != common_diff:
return False
return True
def harmonic_mean(series: list) -> float:
"""
return the harmonic mean of seriesView on GitHub (pinned to f5988cc097)
Solutions
- Check truthiness before calling: if series and is_harmonic_series(series)
- Validate data length at ingestion time and reject empty input upstream
- Provide a sensible default or skip processing when empty
Example fix
# before result = is_harmonic_series(data) # after result = bool(data) and is_harmonic_series(data)
Defensive patterns
Strategy: validation
Validate before calling
result = bool(series) and is_harmonic_series(series)
Type guard
def is_non_empty_list(value: object) -> bool:
return isinstance(value, list) and len(value) > 0 Prevention
- Check len(series) > 0 in caller loops
- Reject empty parsed input early with a clear message
- Single-element lists are valid; only empty ones raise
When it happens
Trigger: Calling is_harmonic_series([]). Any code path that constructs the series dynamically (parsing, filtering, slicing) and yields an empty list will hit this.
Common situations: Parsing '1,2,3' style input where the string was empty; slicing a list with bad indices; filtering all elements out. Single-element lists are fine ([x] with x != 0 returns True).
Related errors
- Input list must be a non empty list
- Length must be a positive integer.
- {num}: Invalid input, please enter a positive integer.
- max_set_length must be non-negative
- Number {n} must instead be a positive integer
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a53da48d191219d1.
Report an issue: GitHub.