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

Posted in /  

Python ValueError: max() arg is an empty sequence Solution
vinaykhatri

Vinay Khatri
Last updated on March 29, 2024

    Python max() is an inbuilt function that can accept an iterable object and return the largest value from it. Instead of an iterable object, we can also pass more than one argument value to the max() function, and it will return the largest value. But if we pass an empty iterable object like an empty list, empty string, empty tuple, or empty set in the max function, it will throw the Error ValueError: max() arg is an empty sequence .

    In this Python tutorial, we will discuss this error statement in detail and learn how to solve it. We will also walk through an example that will demonstrate this error, and in the solution section, we will solve that error.

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

    In Python, we often use max() and min() functions to get the largest and smallest value from a list, tuple, and string. And instead of writing a simple comparison operation, we can use the max() or min() methods to find out the minimum and maximum values.

    The max() function will only work if we pass a non-empty iterable object as an argument, and all the values of that iterable object must be of the same data type. If we pass an empty iterable object as an argument value to the max() method, we will encounter the ValueError: max() arg is an empty sequence Error.

    Now Let's discuss the Error statement in detail. The Error statement can further be divided into two parts

    1. ValueError (Exception Type)
    2. max() arg is an empty sequence

    1. ValueError

    The ValueError is one of the Python standard exceptions. It is raised in a Python program when we specify the right argument data type to a function, but the value of that argument is wrong. We can pass iterable objects to the max() method, but if the iterable object is empty, it raises the ValueError Exception.

    2. max() arg is an empty sequence

    max() arg is an empty sequence is the Error Message, it is raised along with the ValueError to tell the programmer more specific detail about the error. This error message tells us that the iterable sequential argument we passed to the max() method is an empty object.

    Example

    my_nums = []  #empty string
    
    largest_num = max(my_num)

    Output

    Traceback (most recent call last):
       File "<stdin>", line 3, in <module>
    ValueError: max() arg is an empty sequence

    Common Example Scenario

    Now we know why this error raises in a Python program. Let's discuss an example of how we can solve this error. We have a prices list that is supposed to contain the prices of the different products. And we need to create a program that asks the user to enter all the prices of the product they brought from the store. And return the largest value price from the prices list.

    Let's say if the user buys 0 products from the store, in that case, if we apply the max() method on our prices list we will get the error.

    Error Example

    # list of all prices
    prices =[]
    
    # number of products
    products = 0
    
    for number in range(products):
        price = float(input(f"Enter the price of product {number +1}"))
        # append the price in the prices list
        prices.append(price)
    
    # find the largest price
    most_expensive = max(prices)
    
    print("The most expensive product price is: ",most_expensive )

    Output

    Traceback (most recent call last):
      File "C:\Users\tsmehra\Desktop\code\main.py", line 13, in 
        most_expensive = max(prices)
    ValueError: max() arg is an empty sequence

    Break the code

    In this example, we are getting this error because the list prices passed to the max() function is empty. The value of products is 0. That's why we are not able to append values to the prices list, which makes a list empty, and the empty list causes the error with the max function.

    Solution

    If you encounter such situations where the list object depends on some other statements, it might be possible that the iterable object can be empty. In such cases, we can specify a default argument value to the max function that will be returned if the iterable object is empty.

    max(iterable_object, default = value)

    Example Solution

    # list of all prices
    prices =[]
    
    # number of products
    products = 0
    
    for number in range(products):
        price = float(input(f"Enter the price of product {number +1}: "))
        # append the price in the prices list
        prices.append(price)
    
    # find the largest price
    most_expensive = max(prices, default = 0.0)
    
    print("The most expensive product price is: ",most_expensive )

    Output

    The most expensive product price is: 0.0

    Wrapping Up!

    The Error ValueError: max() arg is an empty sequence raises in a Python program when we pass an empty iterable object to the max method. To solve this error, we need to make sure that we are passing a non-empty iterable object to the max() method. If the program is all dynamic and the iterable object elements depend on the program run, there we can specify the default argument in the max() method after the iterable object, so in the case of an empty iterable object, the max() method return the default value, not the error.

    If you are still getting this error in your Python program, you can share your code in the comment section. We will try to help you in debugging.

    People are also reading:

    Leave a Comment on this Post

    0 Comments