If you pass an empty list to the bool() function, it will return False. An empty list in an if statement will also return False. If you pass an empty list to the len() function, it will return 0.
In this tutorial, we will go through several methods to check if a list is empty in Python with the help of code examples.
Table of contents
Iterating Over a List in Python
Lists are one of the four built-in data types in Python used to store collections of data. List objects are iterable, meaning you can traverse the items in a list to retrieve their values or perform operations on them. The simplest way to iterate over the items in a list is to use a for loop. You cannot iterate over an empty list; therefore, it is good practice to check if a list is empty before attempting to iterate over it. Let’s go through the methods to check if a list is empty in Python.
Using the PEP8 Truth Value Testing Method
In Python, there is a method called Truth Value Testing. We can use the truth value in an if or while condition or as the operand of Boolean operations, and an empty list will have a truth value of False. You can read more about Truth Value Testing by going to this documentation.
Let’s look at an example of checking two lists using truth value testing:
list_1 = ["Python", "is", "fun", "to", "learn", "!"]
list_2 = []
if list_1:
for item in list_1:
print(item)
else:
print("List is empty")
if list_2:
for item in list_2:
print(item)
else:
print("List is empty")
In the above code, we define two lists, one contains string items, and the other is empty. We use the truth value of each of the lists in an if statement. If the if statement returns true, then the code iterates over the list and prints the list items. Otherwise, it prints that the list is empty. Let’s run the code to get the result:
Python
is
fun
to
learn
!
List is empty
The above result shows us that the first list is not empty and the second list is empty.
We can also use if not to check the truth value of a list, as follows:
list_1 = ["Python", "is", "fun", "to", "learn", "!"]
list_2 = []
if not list_1:
print("List is empty")
else:
for item in list_1:
print(item)
if not list_2:
print("List is empty")
else:
for item in list_2:
print(item)
Let’s run the code to get the result
Python
is
fun
to
learn
!
List is empty
Using the bool() Function
We can pass the truth value of a list to the bool() function to check whether the list is empty or not. Let’s look at an example of checking if two lists are empty using the bool() function.
list_1 = ["Python", "is", "fun", "to", "learn", "!"]
list_2 = []
if bool(list_1):
for item in list_1:
print(item)
else:
print("List is empty")
if bool(list_2):
for item in list_2:
print(item)
else:
print("List is empty")
In the above code, we define two lists, one contains string items, and the other is empty. We pass the truth value of each list to the bool() function within an if statement. If the if statement returns true, then the code iterates over the list and prints the list items. Otherwise, it prints that the list is empty. Let’s run the code to get the result:
Python
is
fun
to
learn
!
List is empty
The above result shows us that the first list is not empty and the second list is empty.
Using the len() Function
If we pass an empty list to the len() function, it will return 0. We can use this 0 length to check if a list is empty using either truth value testing or the comparison operator ==.
In Truth Value Testing, the value of 0 is equal to False. Let’s look at an example of checking if two lists are empty using the len() function with truth value testing:
list_1 = ["Python", "is", "fun", "to", "learn", "!"]
list_2 = []
if len(list_1):
for item in list_1:
print(item)
else:
print("List is empty")
if len(list_2):
for item in list_2:
print(item)
else:
print("List is empty")
In the above code, we define two lists, one contains string items, and the other is empty. We pass each list to the len() function within an if statement. If the if statement returns true, then the code iterates over the list and prints the list items. Otherwise, it prints that the list is empty. Let’s run the code to get the result:
Python
is
fun
to
learn
!
List is empty
Let’s look at an example of checking if two lists are empty using the len() function with the comparison operator ==.
list_1 = ["Python", "is", "fun", "to", "learn", "!"]
list_2 = []
if len(list_1) == 0:
for item in list_1:
print(item)
else:
print("List is empty")
if len(list_2) == 0:
for item in list_2:
print(item)
else:
print("List is empty")
In the above code, we define two lists, one contains string items, and the other is empty. We pass each list to the len() function within an if statement and check if the value returned equals 0. If the if statement evaluates to True, then the code iterates over the list and prints the list items. Otherwise, it prints that the list is empty. Let’s run the code to get the result:
Python
is
fun
to
learn
!
List is empty
Summary
Congratulations on reading to the end of this tutorial! We have gone through various ways to check if a list is empty in Python. It is good practice to check if a list is empty before performing any iterative operations.
The most efficient way to check if a list is empty is to use truth value testing as it only requires an if-statement with the list. However, if you are just starting to learn Python, it may be preferable to use len() == 0 as it is easier to interpret.
For further reading on list manipulation, go to the articles:
- How to Remove Empty Strings from a List of Strings in Python
- How to Count Occurrences of an Element in a Python List
- How to Convert a List to a String in Python
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.