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

Python provides the “min()” function for finding the maximum value of any input sequences such as lists, tuples, dicts, etc. The “min()” function will throw an error when an empty sequence is passed as an argument. To encounter this error, we can use multiple solutions in Python.

This write-up will give you different reasons and solutions for Python’s “min() arg is an empty sequence” error with the following outline:

So, let’s get started!

Reason: Passing an Empty Sequence

The prominent reason that causes this error in Python is when a user tries to pass an empty sequence as an argument to a variable named “dict_value“.

The above snippet shows “ValueError” because the variable “dict_value” is an empty sequence and is passed as an argument to the “min()” function.

Solution 1: Pass Default Keyword Argument

To fix this error, we need to pass the “default” keyword argument and assign it the value “0”. The following example code demonstrates this scenario:

Code:  

dict_value = {}
output = min(dict_value, default = 0)
print(output)

In the above code, the “min()” function accepts two parameters. The first parameter takes the sequence variable as an argument. The second parameter takes the “default” value as an argument. So when the “min()” function is applied to an empty sequence, the default value is returned as an output.

Output:

The above output shows a “0” value because the input sequence is empty.

Solution 2: Checking Sequence Value

We can also resolve this error by checking the sequence value at the program’s start using the “if-else” statement.

Code:

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

In the above code, the “if” statement is used along with the “len()” function to check the length of the input sequence. If the length of the sequence is greater than “0”, then the “min()” function is applied, and it retrieves the minimum items. But if the length is smaller than “0”, then the else block is executed in a program.

Output:

The min() function can not find the minimum result because the input sequence is empty.

Solution 3: Use try-except Block

The error “min() arg is an empty sequence” can also be resolved using the “try-except” block in Python.

Code:

dict_value = {}
try:
    Output = min(dict_value)
    print(Output)
except:
    print('Input Sequence is Empty')

In the above code, the “try” block executes the “min()” function on the input sequence when the input sequence is not empty. But when the sequence is empty and an error is raised, the “except” block is executed in the program to handle this error.

Output:

The except block shows the exception message when the “min()” function argument is passed with an empty sequence value.

Solution 4: Use None Default Value as Argument

The “None” value can also be defined as an argument value of the “min()” function. So, when an empty sequence is passed as an argument to the “min()” function, it will return the “None” value by default.

Code;

dict_value = {}
output = min(dict_value, default = None)
print(output)

In the above code, the “None” value is assigned to the “min()” function parameter “default”.

Output:

The above output shows a “None” value, which indicates that the input sequence is empty.

Solution 5: Passing the Value in Sequence

Another straightforward solution is to pass some value inside the sequence when you find that the sequence initialized in the program is empty.

Code:

dict_value = {11: 'ALex', 22: 'Lily'}
output = min(dict_value, default = None)
print(output)

In the above code, the dictionary variable “dict_value” contains element values initialized in the program. The min() function will return the minimum value of the key in the dictionary.

Output:

The above output shows the minimum key value of “11” in the input dictionary.

Conclusion

The “min() arg is an empty sequence” error arises when a user tries to pass an empty sequence as an argument to the min() function. To fix this error, different solutions are used in Python, such as using default parameters, the None value as the default parameter, the try-except block, etc. The “if-else” statement can be utilized along with the combination of the “len()” function to resolve this error. This article has provided multiple solutions for the error “min() arg is an empty sequence” in Python.