TheAlgorithms/Python · error · ValueError

Input series is not valid, valid series - [1, 2/3, 2]

Error message

Input series is not valid, valid series - [1, 2/3, 2]

What it means

Raised by is_harmonic_series() in maths/series/harmonic.py when the argument is not a list instance. The function needs to index elements and build a reciprocal series, so it requires a genuine Python list. The message's example '[1, 2/3, 2]' merely illustrates a valid harmonic series, not the expected literal input.

Source

Thrown at maths/series/harmonic.py:41

    >>> is_harmonic_series(4)
    Traceback (most recent call last):
        ...
    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:

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to a list before calling: is_harmonic_series(list(series))
  2. Verify the variable is a list at the call site with isinstance debugging
  3. Trace where the value originates and fix the producer to build a list

Example fix

# before
is_harmonic_series((1, 2, 3))  # tuple -> ValueError

# after
is_harmonic_series([1, 2, 3])  # -> True
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(series, list):
    raise TypeError(f'expected list, got {type(series).__name__}')
is_harmonic_series(series)

Type guard

def is_series_list(value: object) -> bool:
    return isinstance(value, list)

Prevention

When it happens

Trigger: Calling is_harmonic_series(4), is_harmonic_series('series'), or passing a tuple/other sequence type. The isinstance(series, list) check is the first guard, so any non-list fails here regardless of contents.

Common situations: Dynamic typing mistakes where a scalar or string flows into a function expecting a list; passing tuples from other APIs; unpacking mistakes like passing a variable that was never converted to list.

Related errors


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