TheAlgorithms/Python · error · ValueError
The list is empty. Provide a non-empty list.
Error message
The list is empty. Provide a non-empty list.
What it means
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).
Source
Thrown at maths/interquartile_range.py:54
Return the interquartile range for a list of numeric values.
:param nums: The list of numeric values.
:return: interquartile range
>>> interquartile_range(nums=[4, 1, 2, 3, 2])
2.0
>>> interquartile_range(nums = [-2, -7, -10, 9, 8, 4, -67, 45])
17.0
>>> interquartile_range(nums = [-2.1, -7.1, -10.1, 9.1, 8.1, 4.1, -67.1, 45.1])
17.2
>>> interquartile_range(nums = [0, 0, 0, 0, 0])
0.0
>>> interquartile_range(nums=[])
Traceback (most recent call last):
...
ValueError: The list is empty. Provide a non-empty list.
"""
if not nums:
raise ValueError("The list is empty. Provide a non-empty list.")
nums.sort()
length = len(nums)
div, mod = divmod(length, 2)
q1 = find_median(nums[:div])
half_length = sum((div, mod))
q3 = find_median(nums[half_length:length])
return q3 - q1
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
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.
Example fix
// before
iqr = interquartile_range(nums=group_data) # some groups are empty
// after
if not group_data:
continue # or handle empty-group policy
iqr = interquartile_range(nums=group_data) Defensive patterns
Strategy: validation
Validate before calling
if not nums:
raise ValueError("cannot compute IQR of empty data")
iqr = interquartile_range(nums=nums) Type guard
def is_non_empty_list(v) -> bool:
return isinstance(v, list) and len(v) > 0 Try / catch
try:
iqr = interquartile_range(nums=group)
except ValueError as e:
if 'empty' in str(e):
iqr = None # mark group as having no statistic
else:
raise Prevention
- Skip empty groups when aggregating
- Pass a copy if you must preserve order: interquartile_range(nums=list(data)) since the function sorts in place
When it happens
Trigger: Calling interquartile_range(nums=[]) or interquartile_range(nums=None). Any empty or falsy nums argument triggers the guard before sorting.
Common situations: 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.
Related errors
- Missing an input
- surface_area_cube() only accepts non-negative values
- surface_area_cuboid() only accepts non-negative values
- surface_area_sphere() only accepts non-negative values
- surface_area_hemisphere() only accepts non-negative values
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c7be32b1028b3178.
Report an issue: GitHub.