The min() function is built into Python and returns the item with the smallest value in an iterable or the item with the smallest value from two or more objects of the same type. When you pass an iterable to the min() function, such as a list, it must have at least one value to work. If you use the min() function on an empty list, you will raise the error “ValueError: min() arg is an empty sequence”.

To solve this error, ensure you only pass iterables to the min() function with at least one value. You can check if an iterable has more than one item by using an if-statement, for example,

if len(iterable) > 0: 
    min_value = min(iterable)

This tutorial will go through the error in detail how to solve it with a code example.


ValueError: min() arg is an empty sequence

What is a Value Error in Python?

In Python, a value is a piece of information stored within a particular object. You will encounter a ValueError in Python when you use a built-in operation or function that receives an argument with the right type but an inappropriate value. Let’s look at an example of a ValueError:

value = 'string'

print(float(value))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
print(float(value))

ValueError: could not convert string to float: 'string'

The above code throws the ValueError because the value ‘string‘ is an inappropriate (non-convertible) string. You can only convert numerical strings using the float() method, for example:

value = '5'
print(float(value))
5.0

The code does not throw an error because the float function can convert a numerical string. The value of 5 is appropriate for the float function.

The error ValueError: min() arg is an empty sequence is a ValueError because while an iterable is a valid type of object to pass to the min() function, the value it contains is not valid.

Using min() in Python

The min() function returns the smallest item in an iterable or the smallest of two or more arguments. Let’s look at an example of the min() function to find the minimum of three integers:

var_1 = 2
var_2 = 7
var_3 = 1

min_val = min(var_1, var_2, var_2)

print(min_val)

The arguments of the min() function are the three integer variable. Let’s run the code to get the result:

2

Let’s look at an example of passing an iterable to the min() function. In this case, we will use a string. The min() function finds the minimum alphabetical character in a string.

string = "research"

min_val = min(string)

print(min_val)

Let’s run the code to get the result:

s

When you pass an iterable to the min() function, it must contain at least one value. The min() function cannot return the smallest item if no items are present in the list. The same applies to the max() function, which finds the largest item in an iterable.

Example: Returning a Minimum Value from a List using min() in Python

Let’s write a program that finds the minimum boxes sold of different vegetables across a week. First, we will define a list of vegetables:

vegetables = [

{"name":"Spinach", "boxes_sold":[10, 4, 20, 50, 29, 100, 70]},

{"name":"Asparagus", "boxes_sold":[20, 5, 10, 50, 90, 10, 50]},

{"name":"Broccolli", "boxes_sold":[33, 10, 8, 7, 34, 50, 21]},

{"name":"Carrot", "boxes_sold":[]}

]

The list contains four dictionaries. Each dictionary contains the name of a vegetable and a list of the number of boxes sold over seven days. The carrot order recently arrived, meaning no carrots were sold. Next, we will iterate over the list using a for loop and find the smallest amount of boxes sold for each vegetable over seven days.

for v in vegetables:

    smallest_boxes_sold = min(v["boxes_sold"])

    print("The smallest amount of {} boxes sold this week is {}.".format(v["name"], smallest_boxes_sold))

We use the min() function in the above code to get the smallest item in the boxes_sold list. Let’s run the code to get the result:

The smallest amount of Spinach boxes sold this week is 4.
The smallest amount of Asparagus boxes sold this week is 5.
The smallest amount of Broccolli boxes sold this week is 7.
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [9], in <cell line: 1>()
      1 for v in vegetables:
----> 3     smallest_boxes_sold = min(v["boxes_sold"])
      5     print("The smallest amount of {} boxes sold this week is {}.".format(v["name"], smallest_boxes_sold))

ValueError: min() arg is an empty sequence

The program raises the ValueError because the boxes_sold list for Carrot is empty.

Solution

To solve this error, we can add an if statement to check if any boxes were sold in a week before using the min() function. Let’s look at the revised code:

for v in vegetables:

    if len(v["boxes_sold"]) > 0:

        smallest_boxes_sold = min(v["boxes_sold"])
        print("The smallest amount of {} boxes sold this week is {}.".format(v["name"], smallest_boxes_sold))

    else:

        print("No {} boxes were sold this week.".format(v["name"]))
The smallest amount of Spinach boxes sold this week is 100.
The smallest amount of Asparagus boxes sold this week is 90.
The smallest amount of Broccolli boxes sold this week is 50.
No Carrot boxes were sold this week.

The program successfully prints the minimum amount of boxes sold for Spinach, Asparagus, and Broccolli. The boxes_sold list for Carrot is empty; therefore, the program informs us that no boxes of carrots were sold this week.

Summary

Congratulations on reading to the end of this tutorial! The error: “ValueError: min() arg is an empty sequence” occurs when you pass an empty list as an argument to the min() function. The min() function cannot find the smallest item in an iterable if there are no items. To solve this, ensure your list has items or include an if statement in your program to check if a list is empty before calling the min() function.

Go to the Python online courses page to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!