How To Check If List Is Empty In Python

Similar to the previous post on how to check if a string is empty the same principles and methods apply when checking if a list is empty in Python.

To check if a list is empty either use the direct approach by using an expression where a list is compared to an empty list or use the boolean expression with a not operator (i.e. if not my_empty_list: ) or use the built-in function len() to see if the length of the list is 0.

Knowing which approach is best depends on how certain you are on the data type of the variable being checked and whether it could ever be something else, and how readable and clear you want your code to be.

Let’s look at each approach in a little more detail, but first let’s define the most important term in this post:

What do you mean by “ empty “?

Before you progress any further it would help if you know what I mean by the term empty . In this article empty will mean that there is nothing contained in the list . If there is something in the list, even if it’s None or False or 0 or an empty string or even an empty list these are all something .

Therefore, the following are not considered empty lists in this article:

[None]
[False]
[0]
[""]
[[]]

An empty list is just this: []

Now that this established in the article let’s look at how you can determine whether a list in your code is empty. The first approach is the cleanest and clearest of the other two and I call this the direct approach.

Check If List Is Empty By Comparing To A List Literal

There are two approaches in this method to check if a list is empty. The first is comparing your list to a list literal .

What Is A List Literal ?

There are two ways of initiating a list in Python code, and one of these ways is called a list literal . A list literal is where you assign a list using the square bracket notation, something like this: my_list = [1, 2, 3]

For the curious, the second way of creating a list when assigning it to a variable is to use the built-in list() function , something like this: my_list = list((1, 2, 3)) . The list() function takes one optional parameter, an iterable, and converts it into a List data type . If no parameter is provided it returns an empty list ! Perfecto!

Does either approach produce the intended outcome sought? Here’s a quick test:

>>> [] === list()
True
>>> list() === []
True

Notice how the above code produces equality whether you use the list literal and the list() function approach.

Therefore, we can use either approach in an expression to determine if the list you have defined in a variable is empty.

Here are some examples of each approach below:

>>> my_empty_list = []
>>> my_empty_list == []
True
>>> my_empty_list == list()
True

As illustrated in the simple example above the variable my_empty_list is assigned to an empty list and then compared to a list literal and then the result of the built-in list() function. All results produce the correct result.

The reason why I like this code is that it’s clear and concise. Another reason I like this approach is that if the variable used in the expression isn’t a list it will not go through. Have a look at the next couple of examples below to illustrate the point.

>>> my_var = 0
>>> my_var == []
False
>>> my_var = False
>>> my_var == list()
False
>>> my_var = {}
>>> my_var == []
False
>>> my_var = [[]]
>>> my_var == []
False

Notice how irrespective of the data type of the variable my_var when compared against a list literal or the list() function as an expression it produces the same result.

This is the desired result.

Therefore, the preferred approach when determining if a list is empty is to compare it by using either == [] (a list literal) or == list() . My personal preference is the expression using the list literal as it requires fewer characters and is short and succinct.

Check If List Is Empty Use len() Function

Another popular way of checking if a list is empty is to use the built-in function len() which is short for length . The len() function only has one parameter that is required and this parameter either needs to be a sequence (i.e. string, tuple, list or range) or a collection (i.e. dictionary or set) and outputs the length of these objects.

In the case of this article as lists are being used the len() function when used with a list in its parameter will produce the length of that list. Here are a couple of examples:

>>> my_list = [1, 2, 3]
>>> len(my_list)
3
>>> my_empty_list = []
>>> len(my_empty_list)
0

From the two examples above you can see the result of the len() function. It counts the number of items in the list and outputs this number. If a list is empty then the result is 0.

Therefore, to use this approach the expression of the Python code would need to be as follows:

>>> my_empty_list = []
>>> len(my_empty_list) == 0
True

As you can see we have converted the list to an integer based on its length. To check if the list is empty then this would mean the result to be zero.

However, there is a caution with using this approach. If you’re uncertain of the data type of the variable being used in the len() function then this expression could cause problems. Have a look at the following examples:

>>> my_empty_list = ""
>>> len(my_empty_list) == 0
True
>>> my_empty_list = {}
>>> len(my_empty_list) == 0
True
>>> my_empty_list = ()
>>> len(my_empty_list) == 0
True

As you can see from the examples above you are going to run into some problems if the only expression used is just len(x) == 0 because if x happens to be something other than a list then this could cause issues further in your Python code.

One way of getting around this issue is to add another expression to check the data type of the variable used in the len() function : type(x) == list

The built-in type() function takes either one required parameter or three required parameters. With the use case in this running example by using the single parameter it takes an object and returns the type object . Here is a few simple examples of what this function returns:

>>> type([])
<class 'list'>
>>> type(False)
<class 'bool'>
>>> type(0)
<class 'int'>

Therefore, when using this function you can use it in an expression like so to help determine what you’re dealing with:

>>> type([]) == list
True
>>> type(False) == bool
True
>>> type(0) == int
True

To combine the above with our original len() function helps to make sure you’re dealing with a list, and not something else that could sneak through:

>>> my_empty_list = []
>>> len(my_empty_list) == 0 and type(my_empty_list) == list
True

What would happen to the false results tested previously?

>>> my_var = ""
>>> len(my_var) == 0 and type(my_var) == list
False
>>> my_var = {}
>>> len(my_var) == 0 and type(my_var) == list
False
>>> my_var = ()
>>> len(my_var) == 0 and type(my_var) == list
False
>>> my_var = [[]]
>>> len(my_var) == 0 and type(my_var) == list
False

Check If List Is Empty Use bool() Function

Another approach to check if a list is empty in Python is to convert it to a simple boolean. This approach also has two means which I’ll define as the implicit approach or the explicit approach.

The explicit approach with checking if a list is empty is to use the built-in bool() function in Python. This function takes a single optional parameter and converts it to either True or False .

By default if the bool() function has no parameters it returns False .

>>> bool() == True
False
>>> bool() == False
True

Interestingly when a list is inserted into the single parameter with the bool() function it returns False if the list is empty:

>>> bool([])
False
>>> bool(list())
False
>>> bool([""])
True
>>> bool([None])
True
>>> bool([[]])
True

As demonstrated above with the following examples, the first results return False as these are empty lists . To use this result in an expression you would need to use two further approaches: using the not operator or explicitly using the == False expression.

>>> bool([]) == False
True
>>> not bool([])
True

Either approach is fine provided the expression is clear in your Python code.

What about the implicit approach?

The implicit approach doesn’t involve using the bool() built-in function but instead knows that when a variable is used in an expression and in an if statement that the contents of the variable are converted to a boolean expression indirectly .

Here’s an example of this:

>>> my_empty_string = []
>>> if not my_empty_string:
...     print("List is empty")
...
List is empty

As the variable my_empty_string which is an empty list is converted to False (as seen further above when it was inserted into the bool() built-in function) Python converts the expression not my_empty_string into a boolean expression to determine whether the condition is True or False . As the condition is True the contents of print are output to the REPL screen.

The issue with both the explicit and the implicit approach is that if the variable is not a list then the condition could still sneak through your checks. This is the same problem as mentioned above when using the list literals and the built-in list() function.

The same problems occur here and the best way to combat them is to add the express and type(x) == list to your explicit or implicit boolean approach when checking if a list is empty.

Summary

To check if a list is empty the most succinct and clearest approach is to use a list literal in the expression, like my_list == [] . This is the clearest because no additional type checks need to be performed and its the most succinct because the alternative requires replacing the list literal with the built-in list() function: my_list == list() .

Other approaches are available which can be commonly found in Python code such as using the length function len() to obtain the number of elements in the list, with 0 representing no items and therefore an empty list.

The other approach using booleans can seem a little oblique if the implicit method is used as there’s little information to read in the code. Whereas the explicit approach clearly shows the list is being converted to a boolean data type.

Both the len() and boolean approaches have weaknesses and an additional type() check is needed if there’s uncertainty on the result being a list.

Photo of author
Ryan Sheehy
Ryan has been dabbling in code since the late '90s when he cut his teeth exploring VBA in Excel. Having his eyes opened with the potential of automating repetitive tasks, he expanded to Python and then moved over to scripting languages such as HTML, CSS, Javascript and PHP.