TheAlgorithms/Python · error · ValueError

Input series is not valid, valid series - [2, 4, 6]

Error message

Input series is not valid, valid series - [2, 4, 6]

What it means

is_arithmetic_series() in maths/series/arithmetic.py checks whether consecutive differences are constant. Its first guard requires isinstance(series, list); any non-list (int, string, tuple, generator) raises ValueError('Input series is not valid, valid series - [2, 4, 6]') before length is inspected. The message shows an example valid series rather than describing the type failure, which surprises callers expecting a TypeError.

Source

Thrown at maths/series/arithmetic.py:30

    """
    checking whether the input series is arithmetic series or not
    >>> is_arithmetic_series([2, 4, 6])
    True
    >>> is_arithmetic_series([3, 6, 12, 24])
    False
    >>> is_arithmetic_series([1, 2, 3])
    True
    >>> is_arithmetic_series(4)
    Traceback (most recent call last):
        ...
    ValueError: Input series is not valid, valid series - [2, 4, 6]
    >>> is_arithmetic_series([])
    Traceback (most recent call last):
        ...
    ValueError: Input list must be a non empty list
    """
    if not isinstance(series, list):
        raise ValueError("Input series is not valid, valid series - [2, 4, 6]")
    if len(series) == 0:
        raise ValueError("Input list must be a non empty list")
    if len(series) == 1:
        return True
    common_diff = series[1] - series[0]
    for index in range(len(series) - 1):
        if series[index + 1] - series[index] != common_diff:
            return False
    return True


def arithmetic_mean(series: list) -> float:
    """
    return the arithmetic mean of series

    >>> arithmetic_mean([2, 4, 6])
    4.0
    >>> arithmetic_mean([3, 6, 9, 12])

View on GitHub (pinned to f5988cc097)

Solutions

  1. Materialize to a list before calling: is_arithmetic_series(list(series)).
  2. If input may be a string of numbers, parse it first (e.g. [int(x) for x in s.split()]).
  3. Catch ValueError, not TypeError, for this function's guards.

Example fix

# before
is_arithmetic_series(data)  # ValueError when data is a tuple or range

# after
is_arithmetic_series(list(data))
Defensive patterns

Strategy: type-guard

Validate before calling

series = list(series) if not isinstance(series, list) else series
if not series:
    raise ValueError('empty series')
is_arithmetic_series(series)

Type guard

def is_series_input(v) -> bool:
    return isinstance(v, list) and len(v) > 0

Try / catch

try:
    is_arithmetic_series(s)
except ValueError as exc:
    if 'not valid' in str(exc):
        s = list(s)
    else:
        raise

Prevention

When it happens

Trigger: Calling is_arithmetic_series(4), is_arithmetic_series('123'), is_arithmetic_series((2, 4, 6)) (tuples are rejected too), or passing a range/iterator/generator object.

Common situations: Passing tuples or ranges that 'feel like' sequences; feeding data from pandas/numpy or a generator without materializing; callers catching TypeError and missing this ValueError.

Related errors


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