TheAlgorithms/Python · error · ValueError

abs_max() arg is an empty sequence

Error message

abs_max() arg is an empty sequence

What it means

Raised by abs_max in maths/abs.py when called with an empty list. Like its sibling abs_min it needs at least one element to seed j = x[0]; the guard raises ValueError first with the message styled after builtin max() on an empty sequence.

Source

Thrown at maths/abs.py:50

    for i in x:
        if abs_val(i) < abs_val(j):
            j = i
    return j


def abs_max(x: list[int]) -> int:
    """
    >>> abs_max([0,5,1,11])
    11
    >>> abs_max([3,-10,-2])
    -10
    >>> abs_max([])
    Traceback (most recent call last):
        ...
    ValueError: abs_max() arg is an empty sequence
    """
    if len(x) == 0:
        raise ValueError("abs_max() arg is an empty sequence")
    j = x[0]
    for i in x:
        if abs(i) > abs(j):
            j = i
    return j


def abs_max_sort(x: list[int]) -> int:
    """
    >>> abs_max_sort([0,5,1,11])
    11
    >>> abs_max_sort([3,-10,-2])
    -10
    >>> abs_max_sort([])
    Traceback (most recent call last):
        ...
    ValueError: abs_max_sort() arg is an empty sequence
    """

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard with emptiness checks: if not values: return None / raise your own domain error.
  2. Ensure the data source produces at least one element before reduction.
  3. Use a default in your own wrapper: abs_max(vals) if vals else fallback.

Example fix

# before
m = abs_max(window)  # window is []

# after
m = abs_max(window) if window else 0
Defensive patterns

Strategy: type-guard

Validate before calling

if not window:
    return 0  # or your domain default
m = abs_max(window)

Type guard

def is_non_empty(seq) -> bool:
    return len(seq) > 0

Prevention

When it happens

Trigger: Calling abs_max([]) - the len(x) == 0 check fires immediately.

Common situations: Empty statistic windows (rolling windows at series start), empty chunks in a parallel map, or datasets where a group-by produced an empty group.

Related errors


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