ValueError: max( ) arg is an empty sequence
In this article, we will learn about the error ValueError: max( ) arg is an empty sequence.
In python, the max( ) method returns the largest or greatest value in an iterable object.
An iterable object could be anything a list, a tuple, a set, or a dictionary.
Let’s understand it more with the help of an example.
Example:
#Creating list 'MyList'
MyList= [ ]
#Using max() method
print(max(MyList))
Output:
file "intobj.py", line 5, in <module>
print(max(MyList))
ValueError: max() arg is an empty sequence
In the above example, we created an empty list i.e a list with no items. Then we passed it as an argument for max( ) method. Then, when we tried compiling the program. We encountered an error in line 5 i.e ValueError: max() arg is an empty sequence. This Is because max( ) method has an empty iterable as an argument.
Solution: How the max method works when passed a non-empty list?
Example:
#Creating list ‘MyList’
MyList= [3,34,123]
#Using max() method
print(max(MyList))
Output:
123
In the above example we created a list with 3 items in it. Then we passed ‘MyList’ as an argument in max( ) method. And the max( ) method returned the largest number among the 3 available numbers or items.