{"record":{"id":"f60da53847c60f0a","repo":"TheAlgorithms/Python","slug":"list-is-empty-f60da5","errorCode":null,"errorMessage":"List is empty","messagePattern":"List is empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/average_absolute_deviation.py","lineNumber":20,"sourceCode":"    \"\"\"\n    Return the average absolute deviation of a list of numbers.\n    Wiki: https://en.wikipedia.org/wiki/Average_absolute_deviation\n\n    >>> average_absolute_deviation([0])\n    0.0\n    >>> average_absolute_deviation([4, 1, 3, 2])\n    1.0\n    >>> average_absolute_deviation([2, 70, 6, 50, 20, 8, 4, 0])\n    20.0\n    >>> average_absolute_deviation([-20, 0, 30, 15])\n    16.25\n    >>> average_absolute_deviation([])\n    Traceback (most recent call last):\n        ...\n    ValueError: List is empty\n    \"\"\"\n    if not nums:  # Makes sure that the list is not empty\n        raise ValueError(\"List is empty\")\n\n    average = sum(nums) / len(nums)  # Calculate the average\n    return sum(abs(x - average) for x in nums) / len(nums)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":2,"sourceCodeEnd":30,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/average_absolute_deviation.py#L2-L30","documentation":"average_absolute_deviation() computes the mean absolute deviation of a list of numbers; it raises ValueError('List is empty') when nums is falsy because the mean (and hence the deviation) is undefined for zero elements.","triggerScenarios":"average_absolute_deviation([]), or passing a list populated by a filter/slice that ended up empty, e.g. average_absolute_deviation([x for x in data if x > 100]).","commonSituations":"Empty datasets after filtering; API responses returning empty arrays; batch jobs where one input group has no rows.","solutions":["Check the list is non-empty before calling; skip or default when it is empty.","Log which input produced the empty list to find the data pipeline issue.","If aggregating many groups, wrap per-group so one empty group doesn't abort the run."],"exampleFix":"# before\nmad = average_absolute_deviation(data)\n\n# after\nmad = average_absolute_deviation(data) if data else 0.0","handlingStrategy":"validation","validationCode":"if not nums:\n    raise ValueError(\"cannot compute average absolute deviation of empty data\")","typeGuard":"def has_elements(nums: list[float]) -> bool:\n    return isinstance(nums, list) and len(nums) > 0","tryCatchPattern":"try:\n    mad = average_absolute_deviation(nums)\nexcept ValueError as e:\n    if str(e) == \"List is empty\":\n        mad = 0.0  # or skip record\n    else:\n        raise","preventionTips":["Check truthiness of collections before statistical calls","Log the source of empty lists to catch upstream fetch failures"],"tags":["math","statistics","empty-collection"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}