The max() method only works if you pass a sequence with at least one value into the method.
If you try to find the largest item in an empty list, you’ll encounter the error “ValueError: max()
arg is an empty sequence”.
In this guide, we talk about what this error means and why you may encounter it. We walk through an example to help you figure out how to resolve this error.
ValueError: max() arg is an empty sequence
The max()
method lets you find the largest item in a list. It is similar to the min()
method which finds the smallest item in a list.
For this method to work, max()
needs a sequence with at least one value. This is because you cannot find the largest item in a list if there are no items. The largest item is non-existent because there are no items to search through.
A variation of the “ValueError: max()
arg is an empty sequence” error is found when you try to pass an empty list into the min()
method. This error is “ValueError: min()
arg is an empty sequence”. This min()
error occurs for the same reason: you cannot find the smallest value in a list with no values.
An Example Scenario
We’re going to build a program that finds the highest grade a student has earned in all their chemistry tests. To start, define a list of students:
students = [ { "name": "Ron", "grades": [75, 92, 84] }, { "name": "Katy", "grades": [92, 86, 81] }, { "name": "Rachel", "grades": [64, 72, 72] }, { "name": "Miranda", "grades": [] } ]
Our list of students contains four dictionaries. These dictionaries contain the names of each student as well as a list of the grades they have earned. Miranda does not have any grades yet because she has just joined the chemistry class.
Next, use a for loop to go through each student in our list of students and find the highest grade each student has earned and the average grade of each student:
for s in students: highest_grade = max(s["grades"]) average_grade = round(sum(s["grades"]) / len(s["grades"])) print("The highest grade {} has earned is {}. Their average grade is {}.".format(s["name"], highest_grade, average_grade))
We use the max()
function to find the highest grade a student has earned. To calculate a student’s average grade, we divide the total of all their grades by the number of grades they have received.
We round each student’s average grade to the nearest whole number using the round()
method.
Run our code and see what happens:
The highest grade Ron has earned is 92. Their average grade is 84. The highest grade Katy has earned is 92. Their average grade is 86. The highest grade Rachel has earned is 72. Their average grade is 69. Traceback (most recent call last): File "main.py", line 10, in <module> highest_grade = max(s["grades"]) ValueError: max() arg is an empty sequence
Our code runs successfully until it reaches the fourth item in our list. We can see Ron, Katy, and Rachel’s highest and average grades. We cannot see any values for Miranda.
The Solution
Our code works on the first three students because each of those students have a list of grades with at least one grade. Miranda does not have any grades yet.
Because Miranda does not have any grades, the max()
function fails to execute. max()
cannot find the largest value in an empty list.
To solve this error, see if each list of grades contains any values before we try to calculate the highest grade in a list. If a list contains no values, we should show a different message to the user.
Let’s use an “if” statement to check if a student has any grades before we perform any calculations:
for s in students: if len(s["grades"]) > 0: highest_grade = max(s["grades"]) average_grade = round(sum(s["grades"]) / len(s["grades"])) print("The highest grade {} has earned is {}. Their average grade is {}.".format(s["name"], highest_grade, average_grade)) else: print("{} has not earned any grades.".format(s["name"]))
Our code above will only calculate a student’s highest and average grade if they have earned at least one grade. Otherwise, the user will be informed that the student has not earned any grades. Let’s run our code:
The highest grade Ron has earned is 92. Their average grade is 84. The highest grade Katy has earned is 92. Their average grade is 86. The highest grade Rachel has earned is 72. Their average grade is 69. Miranda has not earned any grades.
Our code successfully calculates the highest and average grades for our first three students. When our code reaches Miranda, our code does not calculate her highest and average grades. Instead, our code informs us that Miranda has not earned any grades yet.
Conclusion
The “ValueError: max()
arg is an empty sequence” error is raised when you try to find the largest item in an empty list using the max()
method.
To solve this error, make sure you only pass lists with at least one value through a max()
statement. Now you have the knowledge you need to fix this problem like a professional coder!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.