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_geometric_series() in maths/series/geometric.py when the input is a list but contains zero elements. The function first checks that the input is a list (non-lists get a different 'Input series is not valid' error), then rejects empty lists because a common ratio cannot be computed from no terms. It is a guard against meaningless input, not a computational failure.

Source

Thrown at maths/series/geometric.py:33

    >>> 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

    >>> geometric_mean([2, 4, 8])
    3.9999999999999996

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check the list is non-empty before calling: if series and is_geometric_series(series): ...
  2. Fix the upstream data source so it produces at least one element
  3. Pass a default sample list such as [2, 4, 8] when the input may be empty

Example fix

# before
print(is_geometric_series(my_list))

# after
if my_list:
    print(is_geometric_series(my_list))
else:
    print('no data')
Defensive patterns

Strategy: validation

Validate before calling

def check_geometric_input(series):
    return isinstance(series, list) and len(series) > 0

if check_geometric_input(series):
    print(is_geometric_series(series))

Type guard

def is_non_empty_number_list(series: object) -> bool:
    return isinstance(series, list) and len(series) > 0 and all(
        isinstance(x, (int, float)) for x in series
    )

Try / catch

try:
    is_geometric_series(series)
except ValueError as e:
    logger.warning('invalid series input: %s', e)

Prevention

When it happens

Trigger: Calling is_geometric_series([]) — any empty list argument. Common when a list is built dynamically (e.g. from user input, file parsing, or a filter/slice) and ends up empty before being passed in.

Common situations: Passing a programmatically generated list that an upstream step emptied (empty file, filter that matched nothing, slice out of range like data[5:3]). Not triggered by non-list types or single-element lists (a one-element list returns True).

Related errors


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