{"record":{"id":"c7be32b1028b3178","repo":"TheAlgorithms/Python","slug":"the-list-is-empty-provide-a-non-empty-list","errorCode":null,"errorMessage":"The list is empty. Provide a non-empty list.","messagePattern":"The list is empty\\. Provide a non-empty list\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/interquartile_range.py","lineNumber":54,"sourceCode":"    Return the interquartile range for a list of numeric values.\n    :param nums: The list of numeric values.\n    :return: interquartile range\n\n    >>> interquartile_range(nums=[4, 1, 2, 3, 2])\n    2.0\n    >>> interquartile_range(nums = [-2, -7, -10, 9, 8, 4, -67, 45])\n    17.0\n    >>> interquartile_range(nums = [-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1])\n    17.2\n    >>> interquartile_range(nums = [0, 0, 0, 0, 0])\n    0.0\n    >>> interquartile_range(nums=[])\n    Traceback (most recent call last):\n    ...\n    ValueError: The list is empty. Provide a non-empty list.\n    \"\"\"\n    if not nums:\n        raise ValueError(\"The list is empty. Provide a non-empty list.\")\n    nums.sort()\n    length = len(nums)\n    div, mod = divmod(length, 2)\n    q1 = find_median(nums[:div])\n    half_length = sum((div, mod))\n    q3 = find_median(nums[half_length:length])\n    return q3 - q1\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":36,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/interquartile_range.py#L36-L68","documentation":"Raised by interquartile_range in maths/interquartile_range.py when nums is an empty list. The IQR is defined as Q3 - Q1 of the sorted data, which is meaningless without observations; additionally the function calls nums.sort() and slices around the midpoint, which would fail or produce nonsense on empty input. The explicit ValueError fires first (note: an empty list is falsy, so 'if not nums' also catches None).","triggerScenarios":"Calling interquartile_range(nums=[]) or interquartile_range(nums=None). Any empty or falsy nums argument triggers the guard before sorting.","commonSituations":"Grouping data by key and hitting an empty group; filters that remove all rows before statistics; ETL pipelines passing empty batches; None defaults flowing through from optional parameters.","solutions":["Check len(data) > 0 before computing and handle the empty case explicitly (skip, impute, or report).","When aggregating groups, skip empty buckets: if not group: continue.","If None is possible, distinguish it from [] and convert missing data to an empty-list skip path."],"exampleFix":"// before\niqr = interquartile_range(nums=group_data)  # some groups are empty\n\n// after\nif not group_data:\n    continue  # or handle empty-group policy\niqr = interquartile_range(nums=group_data)","handlingStrategy":"validation","validationCode":"if not nums:\n    raise ValueError(\"cannot compute IQR of empty data\")\niqr = interquartile_range(nums=nums)","typeGuard":"def is_non_empty_list(v) -> bool:\n    return isinstance(v, list) and len(v) > 0","tryCatchPattern":"try:\n    iqr = interquartile_range(nums=group)\nexcept ValueError as e:\n    if 'empty' in str(e):\n        iqr = None  # mark group as having no statistic\n    else:\n        raise","preventionTips":["Skip empty groups when aggregating","Pass a copy if you must preserve order: interquartile_range(nums=list(data)) since the function sorts in place"],"tags":["math","statistics","iqr","empty-input","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}