{"record":{"id":"3ecb8ed924350aeb","repo":"TheAlgorithms/Python","slug":"find-max-iterative-arg-is-an-empty-sequence","errorCode":null,"errorMessage":"find_max_iterative() arg is an empty sequence","messagePattern":"find_max_iterative\\(\\) arg is an empty sequence","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/find_max.py","lineNumber":20,"sourceCode":"\n\ndef find_max_iterative(nums: list[int | float]) -> int | float:\n    \"\"\"\n    >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):\n    ...     find_max_iterative(nums) == max(nums)\n    True\n    True\n    True\n    True\n    >>> find_max_iterative([2, 4, 9, 7, 19, 94, 5])\n    94\n    >>> find_max_iterative([])\n    Traceback (most recent call last):\n        ...\n    ValueError: find_max_iterative() arg is an empty sequence\n    \"\"\"\n    if len(nums) == 0:\n        raise ValueError(\"find_max_iterative() arg is an empty sequence\")\n    max_num = nums[0]\n    for x in nums:\n        if x > max_num:  # noqa: PLR1730\n            max_num = x\n    return max_num\n\n\n# Divide and Conquer algorithm\ndef find_max_recursive(nums: list[int | float], left: int, right: int) -> int | float:\n    \"\"\"\n    find max value in list\n    :param nums: contains elements\n    :param left: index of first element\n    :param right: index of last element\n    :return: max in nums\n\n    >>> for nums in ([3, 2, 1], [-3, -2, -1], [3, -3, 0], [3.0, 3.1, 2.9]):\n    ...     find_max_recursive(nums, 0, len(nums) - 1) == max(nums)","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/find_max.py#L2-L38","documentation":"Raised by find_max_iterative() in maths/find_max.py when nums is an empty sequence. A maximum of zero elements does not exist, and the algorithm seeds max_num = nums[0], so it rejects empty input up front with ValueError (mirroring built-in max() behavior).","triggerScenarios":"Calling find_max_iterative([]) (per its doctest). The `if len(nums) == 0` guard raises before nums[0] would raise IndexError.","commonSituations":"Finding the max of filtered/aggregated data that can legitimately be empty (no matching rows, empty batches), processing streams where the first chunk is empty, or assuming input is non-empty from a file or API response.","solutions":["Check for empty input first and decide domain semantics (default value, skip, or error).","Use `find_max_iterative(nums) if nums else default` or Python's `max(nums, default=...)` pattern if you switched to built-ins.","Validate collection size at the data-loading boundary so downstream max calls are safe."],"exampleFix":"# before\npeak = find_max_iterative(samples)  # raises when samples == []\n\n# after\npeak = find_max_iterative(samples) if samples else float('-inf')","handlingStrategy":"validation","validationCode":"if not nums:\n    raise ValueError('cannot compute max of empty input')\npeak = find_max_iterative(nums)","typeGuard":"def is_nonempty_seq(nums: object) -> bool:\n    return hasattr(nums, '__len__') and len(nums) > 0","tryCatchPattern":null,"preventionTips":["Decide empty-input semantics (default, skip, error) once at the boundary.","Filter-result lists can be empty even when the source is not — check after filtering.","Prefer built-in max(nums, default=...) when a default is acceptable."],"tags":["math","max","empty-sequence","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}