ValueError: max()/min() arg is an empty sequence in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
6 min

banner

# Table of Contents

  1. ValueError: max() arg is an empty sequence in Python
  2. ValueError: min() arg is an empty sequence in Python

# ValueError: max() arg is an empty sequence in Python

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).

valueerror max arg is an empty sequence

Here is an example of how the error occurs.

main.py
my_list = [] # โ›”๏ธ ValueError: max() arg is an empty sequence result = max(my_list)

using max with an empty list

We passed an empty list to the max() function which caused the error.

# Add values to the list before calling max()

One way to solve the error is to add values to the list before calling the max() function.

main.py
my_list = [] # ๐Ÿ‘‡๏ธ Adding a single value my_list.append(5) # ๐Ÿ‘‡๏ธ Adding multiple values my_list.extend([10, 15, 3]) print(max(my_list)) # ๐Ÿ‘‰๏ธ 15

add values to list before calling max

You can also initialize the list with values.

main.py
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.

# Set the 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.

main.py
my_list = [] result = max(my_list, default=0) print(result) # ๐Ÿ‘‰๏ธ 0

set default keyword argument in call to max

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.

main.py
my_list = [] result = max(my_list, default=None) print(result) # ๐Ÿ‘‰๏ธ None

If the sequence is not empty, the largest value will be returned.

main.py
my_list = [10, 30, 20] result = max(my_list, default=None) print(result) # ๐Ÿ‘‰๏ธ 30

# Using the len() function before calling max()

If you need to check if the list is not empty before calling max(), use an if statement.

main.py
my_list = [] if len(my_list) > 0: result = max(my_list) print(result) else: # ๐Ÿ‘‡๏ธ This runs print('The list is empty.')

using len function before calling max

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.

# Using a try/except statement to handle the error

You can also use a try/except block to handle the error.

main.py
my_list = [] try: result = max(my_list) except ValueError: result = 0 print(result) # ๐Ÿ‘‰๏ธ 0

using try except statement to handle the error

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 also occurs when passing an empty dictionary to max()

The error is also raised if you pass an empty dictionary to the max() function.

main.py
my_dict = {} # โ›”๏ธ ValueError: max() arg is an empty sequence print(max(my_dict))

passing empty dictionary to max

One way to solve the error is to add key-value pairs to the dictionary.

main.py
my_dict = {} my_dict[1] = 'one' my_dict[2] = 'two' print(my_dict) # ๐Ÿ‘‰๏ธ {1: 'one', 2: 'two'} print(max(my_dict)) # ๐Ÿ‘‰๏ธ 2

add key value pairs to the dictionary

You can add key-value pairs to a dictionary using square brackets.

Alternatively, you can initialize the dictionary with key-value pairs.

main.py
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.

main.py
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.

main.py
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.

# How the max() function works in Python

The max function returns the largest item in an iterable or the largest of two or more arguments.

main.py
my_list = [15, 45, 30] result = max(my_list) print(result) # ๐Ÿ‘‰๏ธ 45

The function can also be passed two or more positional arguments.

main.py
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.

main.py
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.

# ValueError: min() arg is an empty sequence in Python

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).

valueerror min arg is an empty sequence

Here is an example of how the error occurs.

main.py
my_list = [] # โ›”๏ธ ValueError: min() arg is an empty sequence result = min(my_list)

calling min with an empty list

We passed an empty list to the min() function which caused the error.

# Add values to the list before calling min()

One way to solve the error is to add values to the list before calling the min() function.

main.py
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.

main.py
my_list = [5, 1, 3, 9] print(min(my_list)) # ๐Ÿ‘‰๏ธ 1

# Specify a default value for when the list is empty

Another way to solve the error is to provide the default keyword argument in the call to the min() function.

main.py
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.

main.py
my_list = [] result = min(my_list, default=None) print(result) # ๐Ÿ‘‰๏ธ None

If the sequence is not empty, the smallest value will be returned.

main.py
my_list = [3, 5, 8] result = min(my_list, default=None) print(result) # ๐Ÿ‘‰๏ธ 3

# Checking if the list is not empty before calling min()

You can also use an if statement to check if the list is not empty before calling min().

main.py
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.

# Using a try/except block to handle the error

You can also use a try/except block to handle the error.

main.py
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.

# How the min() function works in Python

The min function returns the smallest item in an iterable or the smallest of two or more arguments.

main.py
my_list = [10, 5, 20] result = min(my_list) print(result) # ๐Ÿ‘‰๏ธ 5

The function can also be passed two or more positional arguments.

main.py
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.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev