TheAlgorithms/Python · error · ValueError
Both input arrays are empty.
Error message
Both input arrays are empty.
What it means
Raised by find_median_sorted_arrays() in data_structures/arrays/median_two_array.py when both nums1 and nums2 are empty. A median of zero elements is undefined, so the function refuses rather than returning NaN or raising IndexError. Either array being non-empty is fine.
Source
Thrown at data_structures/arrays/median_two_array.py:42
>>> find_median_sorted_arrays([0, 0], [0, 0])
0.0
>>> find_median_sorted_arrays([], [])
Traceback (most recent call last):
...
ValueError: Both input arrays are empty.
>>> find_median_sorted_arrays([], [1])
1.0
>>> find_median_sorted_arrays([-1000], [1000])
0.0
>>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4])
-2.75
"""
if not nums1 and not nums2:
raise ValueError("Both input arrays are empty.")
# Merge the arrays into a single sorted array.
merged = sorted(nums1 + nums2)
total = len(merged)
if total % 2 == 1: # If the total number of elements is odd
return float(merged[total // 2]) # then return the middle element
# If the total number of elements is even, calculate
# the average of the two middle elements as the median.
middle1 = merged[total // 2 - 1]
middle2 = merged[total // 2]
return (float(middle1) + float(middle2)) / 2.0
if __name__ == "__main__":
import doctest
View on GitHub (pinned to f5988cc097)
Solutions
- Check combined emptiness before calling: if not nums1 and not nums2: return None/0/handle.
- Guard aggregation code so a median is only computed when at least one sample exists.
- Treat this as a signal that your filter/window is too strict, not just an exception to suppress.
Example fix
# before med = find_median_sorted_arrays(a, b) # both empty # after med = find_median_sorted_arrays(a, b) if (a or b) else 0.0
Defensive patterns
Strategy: validation
Validate before calling
if not nums1 and not nums2:
return 0.0 # or None, per your convention
median = find_median_sorted_arrays(nums1, nums2) Try / catch
try:
median = find_median_sorted_arrays(nums1, nums2)
except ValueError:
median = float('nan') # explicitly mark no data Prevention
- Guard aggregation code so medians are computed only with at least one sample
- Review filters/windows that can exclude all elements
When it happens
Trigger: Calling find_median_sorted_arrays([], []), or both lists becoming empty after filtering (e.g. both filtered to values above a threshold that nothing meets).
Common situations: Aggregation over filtered datasets where filters can exclude everything, empty time windows in monitoring/metrics code, or initializing accumulators as empty lists and computing a median before adding data.
Related errors
- index out of range
- Invalid value of 'position'
- The parameter s must not be empty.
- The parameter bwt_string must not be empty.
- The parameter idx_original_string must not be lower than 0.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/544de8e380a3940c.
Report an issue: GitHub.