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

is_arithmetic_series() in maths/series/arithmetic.py raises ValueError('Input list must be a non empty list') when it receives an actual list of length 0. The check runs after the isinstance-list guard, so this error specifically means 'correct type, no elements'. A one-element list is legal and returns True (vacuously a series); the empty list has no differences to compare, so it is treated as invalid input rather than vacuously True.

Source

Thrown at maths/series/arithmetic.py:32

    >>> 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])
    7.5
    >>> arithmetic_mean(4)

View on GitHub (pinned to f5988cc097)

Solutions

  1. Skip the call when the list is empty: if series: ... else handle the no-data case explicitly.
  2. Default empty inputs upstream (e.g. treat as False) if your domain defines empty as 'not a series'.
  3. Catch ValueError if emptiness is an expected runtime condition you want to absorb.

Example fix

# before
is_arithmetic_series(window)  # ValueError when window == []

# after
result = is_arithmetic_series(window) if window else False
Defensive patterns

Strategy: validation

Validate before calling

if not series:
    result = False  # or skip; define your empty-window policy
else:
    result = is_arithmetic_series(series)

Type guard

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

Try / catch

try:
    is_arithmetic_series(s)
except ValueError as exc:
    if 'non empty' in str(exc):
        s_is_series = False  # empty input policy
    else:
        raise

Prevention

When it happens

Trigger: Calling is_arithmetic_series([]), or passing a list built from filtering/slicing that legitimately ended up empty (e.g. [x for x in data if x > 0] with no matches).

Common situations: Batch-processing pipelines where some windows/batches are empty; splitting text or data into chunks and processing each; tests that assume empty input returns False or True rather than raising.

Related errors


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