Skip to content

How to Check if a Python List is Empty

How to Check if a Python List is Empty

In this tutorial, you’ll learn how to use Python to check if a list empty. Python lists are one of the most versatile and widely-used container objects. Because Python lists are iterable, you may often want to first check if a list is empty, before attempting to iterate over a list. Without this, your program may run into issues and crash.

By the end of this tutorial, you’ll have learned:

  • How to use Python to check if a list is empty
  • Why you would want to check if a Python list is empty
  • How to check if a list of lists is empty in Python
  • How to check if a NumPy Array is empty

A Quick Recap of Python Lists

Before we dive into learning how to check if a Python list is empty, let’s quickly take a look at what Python lists are and how you can use them. A Python list is a heterogeneous, mutable, and iterable container object in Python.

This means that lists can contain items of different data types, have items added, removed or changed, and that you can loop over these items.

Let’s take a look at a simple list and how we can iterate over a list:

# Iterating Over a Simple List
list1 = [1,2,3,4,5]

for item in list1:
    print(item)

# Returns:
# 1
# 2
# 3
# 4
# 5

We can also create an empty list using either of the methods below:

# Creating Empty Lists in Python
empty1 = []
empty2 = list()

If we were to try and iterate over these lists, nothing would happen. This can lead to some unexpected results. Because of this, checking if a list is empty can be a useful tool to ensure that your program runs as expected.

Checking if a Python List is Empty Using in

The Python PEP 8 document represents a style guide for Python code. Because of this, it includes conventions for coding. Following these conventions can lead to code that’s better understood by others and make use of the language in ways it was intended.

The reason I bring any of this up is that PEP 8 actually provides a convention for checking whether or not a list is empty or not. According to PEP 8, the following code is recommended:

# Using in To Check if a List is Empty
seq = []
if not seq:
   print('List is empty')
if seq:
   print('List is not empty')

Let’s break down how this works: Python assumes that an empty list, just like other empty containers, is represented by False. What we are really writing is if not False.

Remember, PEP-8 provides conventions to make your code more understandable to other programmers. You don’t need to follow this convention and learning about other ways in which this can be done is important to be able to better understand other people’s code.

Checking if a Python List is Empty Using bool

One of the ways you’ll often encounter whether checking if a Python list is empty is using the bool() function. The Python bool() function checks the truthy-ness of an item and returns either True or False. In Python, empty objects are regarded as False.

Let’s see how we can use the bool() function to see a list is empty:

# Using bool() to Check If a List is Empty
empty_list = []

if bool(empty_list) == False:
    print('List is empty')
else:
    print('List is not empty')

# Returns: List is empty

We can see that this function evaluates whether or not a list is empty correctly. However, it’s also redundant. Recall, from our earlier example, that we can evaluate truthy-ness simply by using the if statement.

Checking if a Python List is Empty Using Equality

Another common way to check if a Python list is empty is to compare your list to an empty list using the equality operator ==. This method is quite explicit and, perhaps, beginner-friendly as it makes its intentions very clear.

One benefit of this approach is that it makes it clear that you’re checking that the object is a list, rather than some other item. Let’s see how we can use Python to do this:

# Using Equality to Check if a List is Empty
empty_list = []

if empty_list == []:
    print('List is empty')
else:
    print('List is not empty')

# Returns: List is empty

In the following section, you’ll learn how to use a list’s length to check if its empty.

Checking if a Python List is Empty Using Its Length

In this section, you’ll learn how to check whether a Python list is empty or not by checking its length. Intuitively, a list that is empty has, well, zero items. We can use this information to determine is a list has any items in it.

# Using Length to Check if a List is Empty
empty_list = []

if len(empty_list) == 0:
    print('List is empty')
else:
    print('List is not empty')

# Returns: List is empty

In the following section, you’ll learn how to check if a list of lists of empty.

Checking if a Python List of Lists is Empty

Working with lists of lists adds another layer of complexity, but it’s nothing that we can intuitively solve with Python! The Python any() function checks whether any item in a container is True and will return False if that’s not the case. Because we now know that a Python list is considered False, we can use this to our advantage.

# Using any() to Check if a List of Lists is Empty
empty_list_of_lists = [[], [], []]

if not any(empty_list_of_lists):
    print('List is empty')
else:
    print('List is not empty')

# Returns: List is empty

In the code above, we use the any() function to check if any item is considered True. Because this is not the case, the script correctly lets us know that the list of lists is empty.

Checking if a NumPy Array is Empty

NumPy arrays are similar to lists in Python (even though this is quite the over-simplification). We can check if an array is empty by using its .size attribute, which returns the size of that array. Let’s see how we can accomplish this:

# Using NumPy to Check if an Array is Empty
import numpy as np

arr = np.array([])

if arr.size:
    print('Array is not empty')
else:
    print('Array is empty')

Conclusion

Working with Python lists is an essential skill for a Python developer of any level. Being able to check if a list is empty is a simple, but important task to ensure that your program runs in the way that you want it to. In this tutorial, you learned how to check if a list is empty using truthy-ness, length, and equality. You also learned how to check if lists of lists are empty and whether NumPy arrays are empty.

Additional Resources

To learn more about related topics, check out the tutorials below:

Leave a Reply

Your email address will not be published.