{"record":{"id":"544de8e380a3940c","repo":"TheAlgorithms/Python","slug":"both-input-arrays-are-empty","errorCode":null,"errorMessage":"Both input arrays are empty.","messagePattern":"Both input arrays are empty\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"data_structures/arrays/median_two_array.py","lineNumber":42,"sourceCode":"        >>> find_median_sorted_arrays([0, 0], [0, 0])\n        0.0\n\n        >>> find_median_sorted_arrays([], [])\n        Traceback (most recent call last):\n            ...\n        ValueError: Both input arrays are empty.\n\n        >>> find_median_sorted_arrays([], [1])\n        1.0\n\n        >>> find_median_sorted_arrays([-1000], [1000])\n        0.0\n\n        >>> find_median_sorted_arrays([-1.1, -2.2], [-3.3, -4.4])\n        -2.75\n    \"\"\"\n    if not nums1 and not nums2:\n        raise ValueError(\"Both input arrays are empty.\")\n\n    # Merge the arrays into a single sorted array.\n    merged = sorted(nums1 + nums2)\n    total = len(merged)\n\n    if total % 2 == 1:  # If the total number of elements is odd\n        return float(merged[total // 2])  # then return the middle element\n\n    # If the total number of elements is even, calculate\n    # the average of the two middle elements as the median.\n    middle1 = merged[total // 2 - 1]\n    middle2 = merged[total // 2]\n    return (float(middle1) + float(middle2)) / 2.0\n\n\nif __name__ == \"__main__\":\n    import doctest\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/data_structures/arrays/median_two_array.py#L24-L60","documentation":"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.","triggerScenarios":"Calling find_median_sorted_arrays([], []), or both lists becoming empty after filtering (e.g. both filtered to values above a threshold that nothing meets).","commonSituations":"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.","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."],"exampleFix":"# before\nmed = find_median_sorted_arrays(a, b)  # both empty\n\n# after\nmed = find_median_sorted_arrays(a, b) if (a or b) else 0.0","handlingStrategy":"validation","validationCode":"if not nums1 and not nums2:\n    return 0.0  # or None, per your convention\nmedian = find_median_sorted_arrays(nums1, nums2)","typeGuard":null,"tryCatchPattern":"try:\n    median = find_median_sorted_arrays(nums1, nums2)\nexcept ValueError:\n    median = float('nan')  # explicitly mark no data","preventionTips":["Guard aggregation code so medians are computed only with at least one sample","Review filters/windows that can exclude all elements"],"tags":["value-validation","array","median","statistics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}