{"record":{"id":"8716d2ab15c05a5a","repo":"TheAlgorithms/Python","slug":"abs-max-sort-arg-is-an-empty-sequence","errorCode":null,"errorMessage":"abs_max_sort() arg is an empty sequence","messagePattern":"abs_max_sort\\(\\) arg is an empty sequence","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/abs.py","lineNumber":70,"sourceCode":"    for i in x:\n        if abs(i) > abs(j):\n            j = i\n    return j\n\n\ndef abs_max_sort(x: list[int]) -> int:\n    \"\"\"\n    >>> abs_max_sort([0,5,1,11])\n    11\n    >>> abs_max_sort([3,-10,-2])\n    -10\n    >>> abs_max_sort([])\n    Traceback (most recent call last):\n        ...\n    ValueError: abs_max_sort() arg is an empty sequence\n    \"\"\"\n    if len(x) == 0:\n        raise ValueError(\"abs_max_sort() arg is an empty sequence\")\n    return sorted(x, key=abs)[-1]\n\n\ndef test_abs_val():\n    \"\"\"\n    >>> test_abs_val()\n    \"\"\"\n    assert abs_val(0) == 0\n    assert abs_val(34) == 34\n    assert abs_val(-100000000000) == 100000000000\n\n    a = [-3, -1, 2, -11]\n    assert abs_max(a) == -11\n    assert abs_max_sort(a) == -11\n    assert abs_min(a) == -1\n\n\nif __name__ == \"__main__\":","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/abs.py#L52-L88","documentation":"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.","triggerScenarios":"Calling abs_max_sort([]) - the explicit len(x) == 0 guard fires before sorted(x, key=abs)[-1] is evaluated.","commonSituations":"Same shape as the other abs functions: empty filtered lists, empty batches, or aggregates over groups that turned out empty.","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."],"exampleFix":"# before\nm = abs_max_sort(items)  # items == []\n\n# after\nm = abs_max_sort(items) if items else None","handlingStrategy":"type-guard","validationCode":"if not items:\n    return None\nm = abs_max_sort(items)","typeGuard":"def is_non_empty(seq) -> bool:\n    return len(seq) > 0","tryCatchPattern":null,"preventionTips":["Treat empty group-by groups as skip-cases in aggregation pipelines.","Prefer the O(n) abs_max when input size is uncertain."],"tags":["math","sequence","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}