TheAlgorithms/Python · error · ValueError
abs_max_sort() arg is an empty sequence
Error message
abs_max_sort() arg is an empty sequence
What it means
Raised by abs_max_sort in maths/abs.py when called with an empty list. This variant sorts by absolute value and indexes [-1]; an empty sort has no last element, so the function raises ValueError up front rather than letting the [-1] index raise IndexError.
Source
Thrown at maths/abs.py:70
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
"""
if len(x) == 0:
raise ValueError("abs_max_sort() arg is an empty sequence")
return sorted(x, key=abs)[-1]
def test_abs_val():
"""
>>> test_abs_val()
"""
assert abs_val(0) == 0
assert abs_val(34) == 34
assert abs_val(-100000000000) == 100000000000
a = [-3, -1, 2, -11]
assert abs_max(a) == -11
assert abs_max_sort(a) == -11
assert abs_min(a) == -1
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
Solutions
- Check emptiness before the call and handle the no-data case explicitly.
- Repair upstream filtering/batching so empty inputs cannot reach this helper.
- Prefer the O(n) abs_max unless you need the sort; both share the same empty-input contract.
Example fix
# before m = abs_max_sort(items) # items == [] # after m = abs_max_sort(items) if items else None
Defensive patterns
Strategy: type-guard
Validate before calling
if not items:
return None
m = abs_max_sort(items) Type guard
def is_non_empty(seq) -> bool:
return len(seq) > 0 Prevention
- Treat empty group-by groups as skip-cases in aggregation pipelines.
- Prefer the O(n) abs_max when input size is uncertain.
When it happens
Trigger: Calling abs_max_sort([]) - the explicit len(x) == 0 guard fires before sorted(x, key=abs)[-1] is evaluated.
Common situations: Same shape as the other abs functions: empty filtered lists, empty batches, or aggregates over groups that turned out empty.
Related errors
- abs_min() arg is an empty sequence
- abs_max() arg is an empty sequence
- Limit for the Catalan sequence must be ≥ 0
- Number should not be negative.
- Negative arguments are not supported
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/8716d2ab15c05a5a.
Report an issue: GitHub.