Python: How to Check If a List Is Empty (3 Approaches)

Updated: June 6, 2023 By: Khue Post a comment

In Python, a list is a data structure that can store multiple values of different types in a sequential order. An empty list is a list that contains no elements or items. It is a falsy value.

This short article will show you the most three common ways to check whether a list is empty or not in Python.

Using the len() function

The idea here is simple: use the len() function to check the length of the list. If the length is 0, then the list is empty.

Example:

# Create an empty list
my_list = []

# Check if the list is empty using the len() function
if len(my_list) == 0:
    print("The list is empty")
else:
    print("The list is not empty")

Output:

The list is empty

Using the not operator

You can use the not operator to check the emptiness of a list. This operator returns True if its operand is a falsy value, and False if its operand is a truthy value. As mentioned earlier, an empty list is a falsy value in Python, which means that it evaluates to False in a boolean context. Thus, using the not operator on an empty list will return True.

Example:

# create an empty list
my_list = []

# check if the list is empty using the not operator
if not my_list:
    print("The list is empty")
else:
    print("The list is not empty")

Output:

The list is empty

Using the == operator

The third solution is to compare your list with an empty list ([]] by using the == operator as shown in the example below:

my_list = [] 

if my_list == []:
    print("List is empty")
else:
    print("List is not empty")

Output:

List is empty