{"record":{"id":"296f46ec97ea302f","repo":"TheAlgorithms/Python","slug":"abs-max-arg-is-an-empty-sequence","errorCode":null,"errorMessage":"abs_max() arg is an empty sequence","messagePattern":"abs_max\\(\\) arg is an empty sequence","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/abs.py","lineNumber":50,"sourceCode":"    for i in x:\n        if abs_val(i) < abs_val(j):\n            j = i\n    return j\n\n\ndef abs_max(x: list[int]) -> int:\n    \"\"\"\n    >>> abs_max([0,5,1,11])\n    11\n    >>> abs_max([3,-10,-2])\n    -10\n    >>> abs_max([])\n    Traceback (most recent call last):\n        ...\n    ValueError: abs_max() arg is an empty sequence\n    \"\"\"\n    if len(x) == 0:\n        raise ValueError(\"abs_max() arg is an empty sequence\")\n    j = x[0]\n    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    \"\"\"","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/abs.py#L32-L68","documentation":"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.","triggerScenarios":"Calling abs_max([]) - the len(x) == 0 check fires immediately.","commonSituations":"Empty statistic windows (rolling windows at series start), empty chunks in a parallel map, or datasets where a group-by produced an empty group.","solutions":["Guard with emptiness checks: if not values: return None / raise your own domain error.","Ensure the data source produces at least one element before reduction.","Use a default in your own wrapper: abs_max(vals) if vals else fallback."],"exampleFix":"# before\nm = abs_max(window)  # window is []\n\n# after\nm = abs_max(window) if window else 0","handlingStrategy":"type-guard","validationCode":"if not window:\n    return 0  # or your domain default\nm = abs_max(window)","typeGuard":"def is_non_empty(seq) -> bool:\n    return len(seq) > 0","tryCatchPattern":null,"preventionTips":["Skip empty rolling windows at series start.","Validate batch size > 0 before mapping reducers over chunks."],"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"}