3 Ways to Check if List is Empty Python.

Iterating over lists or any other collections of data, such as dictionaries, or tuples, is a common coding task. But before you do so, it is also quite useful to start by checking if the list you want to iterate – using a for loop or any other loop – is empty. This among other things is the common usage of checking if a list is empty in Python. Whatever reason you may want to check if a list is empty in Python, you will find the 3 methods to do so outlined in this post very useful.

Some methods of checking the emptiness of a list are not favorable, while others are even recommended by Pep 8. I have included them all so that you will have a broader understanding of working with lists and a wider range of options to choose from for whatever problem you are solving. I have arranged this list of methods in the order of their favorability – starting with the most pythonic and ending with the least; I’ll always recommend you use the recommended one – which we will look at in the next section – unless your problem requires a different solution.

3 Ways to Check if List is Empty Python

Table of Contents

1. Check if list is empty in Python Using Boolean Evaluation

Using Boolean Evaluation is the recommended way to check if a list is empty in Python. It is the method recommended in the PEP 8 style guide; You can read more about this under Programming Recommendations. The concept behind this method is that simple datatypes like tuples, lists, strings, etc. evaluate into a boolean value – either True or False – depending on whether the list is empty or not empty. An empty list will evaluate as False, and a list that is not empty will evaluate as the opposite – True. Let’s take a look at one example to check if a list is empty in Python using boolean evaluation.

# empty list & non-empty list
empty_list = []
non_empty_list = [1, 2, 3, 4]

# check if list is empty
def check_list_empty(lst):
    if lst:
        print('The List is not empty')
    else:
        print('The list is empty')
        
# pass in the lists to check_list_empty
check_list_empty(empty_list)
check_list_empty(non_empty_list)
The list is empty
The List is not empty

What we did in the above example was create a function check_list_empty that checks if a list is empty or not. The function takes in a list as its argument. Then using boolean evaluation, it evaluates that list to either True or False.

The first list, empty_list, is an empty Python list; When we pass it to the function, it gets evaluated and because it is empty it will produce a False value. Therefore triggering the else block to run.

That has also been done to non_empty_list, which evaluates to a True because it is not an empty list.

The bool() function can also be used to check if a list is empty in a similar fashion we did previously. bool() evaluates a data type into its boolean value. Therefore, it is just a long and repetitive implementation of what we did before. Let’s take a look at an example:

# empty list & non-empty list
empty_list = []
non_empty_list = [1, 2, 3, 4]

# check if list is empty
def check_list_empty(lst):
    if bool(lst): #changed here
        print('The List is not empty')
    else:
        print('The list is empty')
        
# pass in the lists to check_list_empty
check_list_empty(empty_list)
check_list_empty(non_empty_list)
The list is empty
The List is not empty

It is almost the same as the above example, but more prolonged and more repetitive. It is not Pythonic. There is no need to evaluate the list to its boolean value again using bool() when we can also do so without using it.

2. Check if list is empty in Python Using the len() method

The len() function is used in Python to check the number of items or characters in a data structure. Like the number of characters – including whitespace (check my post on how to trim whitespace in a string) – in a string, the number of key-value pairs in a dictionary, and in this case, the number of list items in a Python list. In this example, we are going to use the len() function to check if a list is empty. If a Python list is empty, the len() method will return a zero (0) otherwise, it will return the number of objects in that list. Let’s take a look:

# empty list & non-empty list
empty_list = []
non_empty_list = [1, 2, 3, 4]

# check if list is empty
def check_list_empty(lst):
    if len(lst) == 0:
        print('The List is empty')
    else:
        print('The list is not empty')
        
# pass in the lists to check_list_empty
check_list_empty(empty_list)
check_list_empty(non_empty_list)
The list is empty
The List is not empty

In our check_list_empty function, we pass the lst argument to the len() function, which checks the length of the list. If the list has a length of 0, this means the list is empty therefore the immediate block will run, however, if it is not it will do otherwise. This method looks good and logical but it is not recommended in the PEP style guide and it is considered non-Pythonic.

3. Check if list is empty in Python by comparing it with an empty list

One last method to check if a list is empty in Python is to compare the list with an empty list. It is similar to the previous one in that we compare values against each other. In the last example, we were checking if the length of a list is zero, in this example, we will be checking if a list is empty because we know what an empty list looks like.

In Python, a list, on the broadest level, is a sequence of comma-separated values, enclosed in an opening square bracket and a closing square bracket. For example, a list of strings could be something this ['check', 'if', 'list', 'is', 'empty', 'in', 'python' ]. That’s a list with 7 items. But an empty list is just a list with no items at all. So it is just a closing and opening square brackets with nothing in them ([]). Thus, we know what an empty list looks like and we can use that knowledge to check if the list we are dealing with is empty. Here’s an example:

# empty list & non-empty list
empty_list = []
non_empty_list = [1, 2, 3, 4]

# check if list is empty
def check_list_empty(lst):
    if lst == []:
        print('The List is empty')
    else:
        print('The list is not empty')
        
# pass in the lists to check_list_empty
check_list_empty(empty_list)
check_list_empty(non_empty_list)
The list is empty
The List is not empty

This one is also a beautiful play of programming skills, however, using it is not suggested. Apart from it being considered unpythonic, it also creates another empty list in your code.

FAQs on 3 Ways to Check if List is Empty Python.

These questions have been asked by fellow developers relating to 3 ways to check if a list is empty in Python.

What is the best way to check if a Python list is empty?

The best way to check if a Python list is empty is to use its boolean equivalent. An empty list evaluates to False, while a list that is not empty evaluates to True. Using an if statement, you can check if a Python list is empty or not.

How to check if a list is not empty python

To check if a list is not empty in Python, you just reverse-engineer the methods we use to check if a list is empty. For example, if an empty list’s boolean value is False, then a non-empty list’s value is True. If the length of an empty list is zero – the one we get when we pass it to len() function – then the length of a non-empty list is not zero. If an empty list is an opening and closing square bracket, with nothing inside [], then a non-empty list is not an opening and closing square bracket.

Is Empty list none Python?

There is a big difference between empty and None. Empty means the number of items in the list is zero while None means the value is not known. Some prefer to ask it like this: ‘Does an empty list == None Python?’ No, it does not!

Is an empty Python list equal to False?

Yes, an empty list evaluates to False. This knowledge forms the main concept behind the best way to check if a list is empty in Python. This method involves checking if a list evaluates to True or to False. If it evaluates to the latter, then the list is empty, otherwise, the list is not empty. You can also check the list’s boolean equivalent by passing it to the bool() function.

Conclusion: 3 Ways to Check if List is Empty Python.

Okay guys, that was it with this post, I really hope you learned something new and important on how to check if a list is empty in Python. Always remember to use the recommended method unless the problem you’re solving requires you do otherwise. There are other multiple ways to check if a list in Python is empty, but the ones I’ve shared with you in this post are the most popular ones. Feel free to do more research if you want to learn about other methods.

Peace!

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 90