How To Resolve ValueError: Min() Arg Is An Empty Sequence In Python 

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

To fix the ValueError: min() arg is an empty sequence in Python, there are some solutions we have effectively tested: Initialize a list of values, pass the Default value to the function, and use if and try/except statements to check for exceptions. Follow the article to understand better.

What causes the ValueError: min() arg is an empty sequence in Python?

The min() function is a built-in function in Python.

The min() function will return the smallest value in an iterable, or the smallest in a parameter passed.

Syntax:

min( value1, value2, value3, … )

Parameters:

  • value1, value2, value3: is a numeric, character, or string expression.

The ValueError: min() arg is an empty sequence in Python that happens when the min() function takes an empty sequence as an argument.

Example: 

emptyList = [ ]

# Use the min() function
print(min(emptyList))

Output:

Traceback (most recent call last):
  File "./prog.py", line 4, in <module>
ValueError: min() arg is an empty sequence

How to solve this error?

Initialize a list of elements

The argument of the min() function is an empty list that will cause an error, so to fix it, you just need to initialize a list of elements. Now the list as an argument to the min() function will not get an error

Example:

  • Initialize a list of elements.
  • Use the min() function to find the smallest element in the list.
# Initialize a list with elements
valuesList = [1, 2, 3, 4]

# Use the min() function
print('The smallest element in the list:', min(valuesList))

Output:

The smallest element in the list: 1

Pass the Default value to the min() function

You can fix the error by passing the value ‘default’ to the min() function. This will replace the default value of an empty list.

Example:

  • Declare an empty list.
  • The min() function with the ‘default=0’ value is used to fix the error.
  • The min() function will return the value of the ‘default’ keyword argument.
myList = []

# Use the min() function with the default value = 0 argument
print(min(myList, default = 0))

Output:

0

You can pass the value default = None as the min() function argument.

Example:

myList = []

# Use the min() function with the default value = None argument
print(min(myList, default = None))

Output:

None

The min() function will return the value of the ‘default’ keyword argument. (If the list has the smallest value, the min() function will return the smallest value).

Use the command if

Before passing an argument to min(), you need to check the length of that argument to avoid an error.

Example:

  • Declare an empty list.
  • Use the if statement to check if the list is empty or not.
myList = []

if myList:
   print(min(myList))
else:
   print('List is empty')

Output:

List is empty

Use len() function.

You can check if a list is empty by using len() and then setting it as a min() function parameter.

Example:

  • Declare a list is empty.
  • Use len() function to check if the list is empty or not. If len() returns zero, the list is empty.
myList = [] 

# Use len() function to check for empty list
if len(myList) == 0:
    print('List is empty')
else:
    print(len(myList))

Output:

List is empty

Use the or operator

Example:

  • Initialize an empty list and a list of elements.
  • The or operator will replace the empty list with a list containing the elements.
firstList = [] 
secondList = [1, 2, 3, 4]

# Use the or operator replace the empty list with a list containing the elements
print("Smallest value:", min(firstList or secondList))

Output:

Smallest value: 1

Summary

The ValueError: min() arg is an empty sequence in Python that is fixed. I think the most suitable way to solve this error is to avoid the empty string initialization. Through this article, I hope you will learn from your experience using the min() function. If you find this helpful website, visit it to read more exciting lessons.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *