ValueError: max() arg is an empty sequence in Python

In Python, an inbuilt function “max()” is used in a program to find the maximum value of the input sequence. Trying to execute the “max()” function on an empty sequence will throw the “max() arg is an empty sequence” error in Python. To resolve this error, various solutions are provided by Python.

This Python write-up will give you the reason and various solutions for “ValueError: max() arg is an empty sequence”.

Reason: Passing Empty Sequence

One of the prominent reasons which cause this error in Python programs is initializing an empty sequence as an argument to the “max()” function.

The above snippet shows “ValueError” because an empty dictionary sequence “dict_value” is passed inside the max() function.

Solution 1: Passing Sequence With Values

To resolve this error, a sequence containing at least one value must be passed to the max() function.

Code:

dict_value = {5:'Lily', 10: 'John'}
Output = max(dict_value)
print(Output)

In the above code, a sequence “dict_value” with multiple elements is initialized in the program. The “max()” function accepts the “dict_value” variable as an argument and returns the dictionary key having the maximum value.

Output:

The above output shows that the max() function retrieves the maximum dictionary key from the input dictionary sequence.

Solution 2: Using the “default” Parameter

This error can also be overcome in Python by assigning the “default” parameter value to “0”.

Code:

dict_value = {}
Output = max(dict_value, default=0)
print(Output)

In the above code, the dictionary variable “dict_value” is initialized with an empty value. The “max()” function accepts two arguments, the input variable “dict_Value” and the default parameter “default=0”. Whenever the input sequence is empty, the “max()” function returns the value “0” by default.

Output:

The above output verified that the input sequence is empty.

Solution 3: Using len() Function

To overcome this error, the “len()” function is also used in Python programs. The “len()” function is used along with the “if-else” statement to get the length and perform the operation on the input sequence based on the specified condition.

Code;

dict_value = {}
if len(dict_value)>0:
    Output = max(dict_value)
    print(Output)
else:
    print('Input Sequence is Empty')

In the above code, the “dict_value” dictionary sequence variable is initialized in the program. The “max()” function will be used on the input sequences if the length of the sequence is greater than “0”; otherwise, the “else” block will execute.

Output:

The above output shows that the input sequence is empty.

Solution 4: Using try-except

The “try-except” is also used to handle the “ValueError: max() arg is an empty sequence” in Python. Let’s see it via the following code:

Code:

dict_value = {}

try: 
    Output = max(dict_value)
    print(Output)
except:
    print('Input Sequence is Empty')

In the above code, the variable “dict_value” is initialized in the program. The “try” block executes its code and finds the maximum value of the input sequence using the “max()” function. But if the error arises, the “except” block will execute its code.

Output:

The above output shows that the “except” block executes its code because the input sequence is empty.

Conclusion

The “ValueError: max() arg is an empty sequence” occurs when a user tries to pass an empty sequence as an argument to the max() function. To resolve this error, various solutions are used in Python, such as passing sequence value, using default parameters, using the len() function, and using the try-except block. The “len()” function can be utilized along with the “if-else” statement to resolve this error. This article delivered multiple solutions for the error “max() arg is an empty sequence” in Python.