Last updated: Apr 8, 2024
Reading timeยท6 min
The Python "ValueError: max() arg is an empty sequence" occurs when we pass an
empty sequence to the max()
function.
To solve the error, provide the default
keyword argument in the call to the
max()
function, e.g. result = max(my_list, default=0)
.
Here is an example of how the error occurs.
my_list = [] # โ๏ธ ValueError: max() arg is an empty sequence result = max(my_list)
We passed an empty list to the max()
function which caused the error.
One way to solve the error is to add values to the list before calling the
max()
function.
my_list = [] # ๐๏ธ Adding a single value my_list.append(5) # ๐๏ธ Adding multiple values my_list.extend([10, 15, 3]) print(max(my_list)) # ๐๏ธ 15
You can also initialize the list with values.
my_list = [5, 10, 15, 3] print(max(my_list)) # ๐๏ธ 15
The list in the example isn't empty, so the error isn't raised.
default
keyword argument in the call to max()
Another way to solve the error is to provide the default
keyword argument in
the call to the max()
function.
my_list = [] result = max(my_list, default=0) print(result) # ๐๏ธ 0
The max()
function will return the value of the default
keyword argument if
the sequence is empty.
You can also set None
as the default value if 0
doesn't suit your use case.
my_list = [] result = max(my_list, default=None) print(result) # ๐๏ธ None
If the sequence is not empty, the largest value will be returned.
my_list = [10, 30, 20] result = max(my_list, default=None) print(result) # ๐๏ธ 30
len()
function before calling max()If you need to check if the list is not empty before calling max()
, use an
if
statement.
my_list = [] if len(my_list) > 0: result = max(my_list) print(result) else: # ๐๏ธ This runs print('The list is empty.')
The len() function returns the length (the number of items) of an object.
The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).
We check if the list has a length greater than 0
and if the condition is met,
we call the max()
function with the list.
try/except
statement to handle the errorYou can also use a try/except block to handle the error.
my_list = [] try: result = max(my_list) except ValueError: result = 0 print(result) # ๐๏ธ 0
If passing the list to the max()
function causes a ValueError
, the except
block is run where we can handle the error.
The list in the example is empty, so the except
block runs where we set the
result
variable to 0
.
The error is also raised if you pass an empty dictionary to the max()
function.
my_dict = {} # โ๏ธ ValueError: max() arg is an empty sequence print(max(my_dict))
One way to solve the error is to add key-value pairs to the dictionary.
my_dict = {} my_dict[1] = 'one' my_dict[2] = 'two' print(my_dict) # ๐๏ธ {1: 'one', 2: 'two'} print(max(my_dict)) # ๐๏ธ 2
You can add key-value pairs to a dictionary using square brackets.
Alternatively, you can initialize the dictionary with key-value pairs.
my_dict = {1: 'one', 2: 'two'} print(my_dict) # ๐๏ธ {1: 'one', 2: 'two'} print(max(my_dict)) # ๐๏ธ 2
The max()
function returns 2
because the max key in the dictionary is 2
.
Alternatively, you can use the default
keyword argument to specify a default
value if the dictionary is empty.
my_dict = {} print(max(my_dict, default=0)) # ๐๏ธ 0
If no default value suits your use case, use a try/except
statement to handle
the error.
my_dict = {} try: result = max(my_dict) except ValueError: # ๐๏ธ This runs print('The dictionary is empty.')
if a value error is raised when passing the dictionary to the max()
function,
the except
block is run.
max()
function works in PythonThe max function returns the largest item in an iterable or the largest of two or more arguments.
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐๏ธ 45
The function can also be passed two or more positional arguments.
result = max(15, 45, 30) print(result) # ๐๏ธ 45
The function takes an optional default
keyword argument which is used to
specify a value to return if the provided iterable is empty.
result = max([], default=0) print(result) # ๐๏ธ 0
If the iterable is empty and the default
keyword argument is not provided, the
function raises a ValueError
.
The Python "ValueError: min() arg is an empty sequence" occurs when we pass an
empty sequence to the min()
function.
To solve the error, provide the default
keyword argument in the call to the
min()
function, e.g. result = min(my_list, default=0)
.
Here is an example of how the error occurs.
my_list = [] # โ๏ธ ValueError: min() arg is an empty sequence result = min(my_list)
We passed an empty list to the min()
function which caused the error.
One way to solve the error is to add values to the list before calling the
min()
function.
my_list = [] # โ Adding a single value my_list.append(5) # โ Adding multiple values my_list.extend([1, 3, 9]) print(min(my_list)) # ๐๏ธ 1
The list.append()
method can be used to add a single value to the list and the
list.extend()
method can be used to add multiple values to the list.
Alternatively, you can initialize the list with values.
my_list = [5, 1, 3, 9] print(min(my_list)) # ๐๏ธ 1
Another way to solve the error is to provide the default
keyword argument in
the call to the min()
function.
my_list = [] result = min(my_list, default=0) print(result) # ๐๏ธ 0
The min()
function will return the value of the default
keyword argument if
the sequence is empty.
You can also specify a None
value if a 0
doesn't suite your use case.
my_list = [] result = min(my_list, default=None) print(result) # ๐๏ธ None
If the sequence is not empty, the smallest value will be returned.
my_list = [3, 5, 8] result = min(my_list, default=None) print(result) # ๐๏ธ 3
You can also use an if
statement to check if the list is not empty before
calling min()
.
my_list = [] if len(my_list) > 0: result = min(my_list) print(result) else: # ๐๏ธ This runs print('The list is empty')
The if
statement uses the len()
function to check that the list has at least
1 element before calling the min()
function.
You can also use a try/except
block to handle the error.
my_list = [] try: result = min(my_list) except ValueError: # ๐๏ธ Handle the error here result = 0 print(result) # ๐๏ธ 0
If passing the list to the min()
function causes a ValueError
, the except
block is run where we can handle the error.
The min function returns the smallest item in an iterable or the smallest of two or more arguments.
my_list = [10, 5, 20] result = min(my_list) print(result) # ๐๏ธ 5
The function can also be passed two or more positional arguments.
result = min(10, 5, 20) print(result) # ๐๏ธ 5
The function takes an optional default
keyword argument which is used to
specify a value to return if the provided iterable is empty.
result = min([], default=0) print(result) # ๐๏ธ 0
If the iterable is empty and the default
keyword argument is not provided, the
function raises a ValueError
.