{"record":{"id":"f26e080b10550434","repo":"TheAlgorithms/Python","slug":"abs-min-arg-is-an-empty-sequence","errorCode":null,"errorMessage":"abs_min() arg is an empty sequence","messagePattern":"abs_min\\(\\) arg is an empty sequence","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/abs.py","lineNumber":30,"sourceCode":"    >>> abs_val(0)\n    0\n    \"\"\"\n    return -num if num < 0 else num\n\n\ndef abs_min(x: list[int]) -> int:\n    \"\"\"\n    >>> abs_min([0,5,1,11])\n    0\n    >>> abs_min([3,-10,-2])\n    -2\n    >>> abs_min([])\n    Traceback (most recent call last):\n        ...\n    ValueError: abs_min() arg is an empty sequence\n    \"\"\"\n    if len(x) == 0:\n        raise ValueError(\"abs_min() arg is an empty sequence\")\n    j = x[0]\n    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    \"\"\"","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/abs.py#L12-L48","documentation":"Raised by abs_min in maths/abs.py when called with an empty list. The function scans for the element with the smallest absolute value; with no elements there is no answer, so it raises ValueError mirroring builtin min()'s error style ('abs_min() arg is an empty sequence').","triggerScenarios":"Calling abs_min([]) - any empty sequence makes len(x) == 0 true and the guard fires before x[0] is accessed.","commonSituations":"Feeding in results of a filter/list comprehension that matched nothing, aggregating over an empty batch in a data pipeline, or processing empty input files line-by-line.","solutions":["Check for emptiness before calling: if not values: handle the empty case explicitly.","Fix the upstream filter so it cannot produce an empty list when at least one element is expected.","If an empty input is valid in your domain, define a sentinel (e.g. None or 0) instead of calling abs_min."],"exampleFix":"# before\nm = abs_min(filtered)  # filtered may be []\n\n# after\nm = abs_min(filtered) if filtered else None","handlingStrategy":"type-guard","validationCode":"if not values:\n    raise ValueError('no values to reduce')\nm = abs_min(values)","typeGuard":"def is_non_empty(seq) -> bool:\n    return len(seq) > 0","tryCatchPattern":null,"preventionTips":["Guard reductions on filtered data with explicit emptiness checks.","Return domain sentinels for empty batches instead of calling helpers."],"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"}