TheAlgorithms/Python · error · ValueError

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

Error message

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

What it means

is_geometric_series() in maths/series/geometric.py checks whether consecutive ratios series[i+1]/series[i] are constant. Its first guard requires isinstance(series, list); anything else — int, string, tuple, generator — raises ValueError('Input series is not valid, valid series - [2, 4, 8]') (the example uses the geometric 2,4,8, unlike the arithmetic sibling's 2,4,6). The check precedes the empty-list check, so a non-list empty-ish input (e.g. '') raises this error, not the non-empty one.

Source

Thrown at maths/series/geometric.py:31

    >>> is_geometric_series([2, 4, 8])
    True
    >>> is_geometric_series([3, 6, 12, 24])
    True
    >>> is_geometric_series([1, 2, 3])
    False
    >>> is_geometric_series([0, 0, 3])
    False
    >>> is_geometric_series([])
    Traceback (most recent call last):
        ...
    ValueError: Input list must be a non empty list
    >>> is_geometric_series(4)
    Traceback (most recent call last):
        ...
    ValueError: Input series is not valid, valid series - [2, 4, 8]
    """
    if not isinstance(series, list):
        raise ValueError("Input series is not valid, valid series - [2, 4, 8]")
    if len(series) == 0:
        raise ValueError("Input list must be a non empty list")
    if len(series) == 1:
        return True
    try:
        common_ratio = series[1] / series[0]
        for index in range(len(series) - 1):
            if series[index + 1] / series[index] != common_ratio:
                return False
    except ZeroDivisionError:
        return False
    return True


def geometric_mean(series: list) -> float:
    """
    return the geometric mean of series

View on GitHub (pinned to f5988cc097)

Solutions

  1. Materialize to list: is_geometric_series(list(series)).
  2. Parse numeric strings into int lists before calling.
  3. Catch ValueError per the module's convention for all input guards.

Example fix

# before
is_geometric_series(seq)  # ValueError when seq is a tuple or range

# after
is_geometric_series(list(seq))
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_geometric_series(series)

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: Calling is_geometric_series(4), is_geometric_series((2, 4, 8)), or passing a range/iterator without materializing. Strings like '248' also land here.

Common situations: Passing tuples from DB rows or unpacking, ranges, or numpy arrays; generic sequence-checking code shared across the arithmetic/geometric modules where only list is accepted; catching TypeError and missing the ValueError.

Related errors


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