3 Simple Ways to Check If a List is Empty in Python

Do you want to know how to check if a list is empty in Python? You are in the right place. Here you will learn three simple ways to do that.

To check if a Python list is empty you can use the len() built-in function that returns the number of elements in a list. You can use this function together with an if/else statement to make the behavior of your application flexible. Two other ways to check for an empty list are the “not operator” and a comparison with an empty list [].

Let’s start with the first way to check for an empty list!

How Do You Check if a List is Empty in Python?

A simple way to check if a list is empty using Python is by using the len() built-in function. This function returns the number of elements in a list (the length of a list). If the number of elements returned by the len() function is zero the list is empty.

First of all, let’s use the Python shell to see how the value returned by the len() function changes depending on the number of elements in a list.

>>> numbers = [1, 2, 3, 4, 5]
>>> len(numbers)
5

>>> numbers = []
>>> len(numbers)
0

You can see that, in the first example, the len() function returns 5, and in the second example, it returns zero. This exactly matches the number of items in the list.

Now let’s write some Python code that uses this logic.

Usually, when writing code for your application you would implement different logic depending on the fact that a list is empty or not. To do that you would use an if else statement.

numbers = [1, 2, 3, 4, 5]

if len(numbers) == 0:
    print("The list is empty")
else:
    print("The list is not empty")

# The list is not empty

The idea is to execute the code in the if branch if the length of the list is 0 otherwise execute the code in the else branch.

Depending on the length of the list we will print a different message. The code above prints that specific message because the list numbers has 5 elements.

Now replace the assignment of the list numbers with the following line of code:

numbers = []

With this syntax, you have assigned an empty list to the variable numbers. Execute the code again and confirm that you see the following output:

The list is empty

In a real application, you would include additional logic in the body of the if and else branches instead of simply printing a message.

One thing to consider with this approach is that this is not the approach suggested by the PEP 8 Style Guide. At the same time, it introduces you to some of the main Python concepts.

Check If a Python List is Empty Using the Boolean not Operator

Now let’s continue with a more Pythonic way to check if lists are empty or not.

A common way to check if a list is empty in Python is to use the not operator. The not operator is a boolean / logic operator that returns True if a list is empty and False if a list is not empty. This is based on the fact that an empty list in Python is considered False. This approach is in line with the PEP 8 Style Guide.

You can use the bool() built-in function to see that Python evaluates an empty list as False and a non-empty list as True.

>>> bool([])
False
>>> bool([1, 2, 3, 4, 5])
True

Now, let’s get familiar with the not operator by checking how it works using the Python shell.

>>> numbers = [1, 2, 3, 4, 5]
>>> not numbers
False

>>> numbers = []
>>> not numbers
True

The test we executed in the Python shell confirms that the “not operator” returns True when a list is empty. This is exactly the condition we want to verify.

Now, in the same way we have done before, we will use an if else statement to show how to use the not operator in a real Python application.

numbers = [1, 2, 3, 4, 5]

if not numbers:
    print("The list is empty")
else:
    print("The list is not empty")

# The list is not empty

This if / else statement checks if the list is empty.

The code in the if statement gets executed if the list is empty. The code in the else statement is executed if the list is not empty.

Then set the list numbers to an empty list and verify the message again:

numbers = []

if not numbers:
    print("The list is empty")
else:
    print("The list is not empty")

# The list is empty

This shows that the not operator works as we expected in determining if a Python list is empty or not.

Verify If a Python List is Empty By Comparing It with an Empty List

You can check whether a list is empty or not in Python by comparing it with an empty list where an empty list is represented by open and closed square brackets []. To compare the list with an empty list you can use the equal operator.

Here is how this would work in the Python shell:

>>> numbers = [1, 2, 3, 4, 5]
>>> numbers == []
False

>>> numbers = []
>>> numbers == []
True

First, you create a list of numbers that is not empty. When you compare this list with the empty list [] the result is a boolean with the value False.

In the second example, you update the list numbers by assigning an empty list to it. When you compare it with an empty list using the equal operator you get back the boolean True because our list is equal to an empty list.

As explained before, you can also integrate this boolean logic with an if / else statement:

if numbers == []:
    print("The list is empty")
else:
    print("The list is not empty")

You now know another way to check if a list is empty using Python.

Conclusion

Well done!

You have completed this tutorial and you now know multiple ways to check if a Python list is empty. This is a common check to perform when writing a Python program.

Also, the advantage of the techniques we have covered in this article to verify if a list is empty is that we didn’t have to import any Python modules. The result is a simpler Python program.

The suggested approach between the three in this article is the one using the “not operator” that is in line with the PEP 8 Style Guide.

Related article: Now that you learned how to check if a list is empty, let’s learn how to add an item to a Python list.

Leave a Comment