How to check if a list is empty
Posted in Python by Dirk - last update: Feb 15, 2024
Using the if-not statement
The most common way to check if a list is empty in Python is to use an if not
statement. If a list is empty the condition evaluates to True
; otherwise, it evaluates to False
.
Example:
my_list = []
if not my_list:
print("The list is empty")
else:
print("The list is not empty")
In this example, if my_list
is empty, the condition if not my_list
will be True
, and the program will print “The list is empty.” If my_list
has elements, the condition will be False
, and it will print “The list is not empty.”
This method also works if the list is None
Note: if the list doesn’t exist (i.e., it is not defined), trying to access or check its content directly will result in a NameError
in Python. To deal with this situation, it’s a good practice to check whether the list exists before attempting to perform any operations on it.
This can be done with a try/except
block
try:
if not mylist:
print("The list is empty")
else:
print("The list is not empty")
except NameError:
print ("The list does not exist")
Alternative methods:
Using len() function
The len()
function returns the number of elements in a list. If the length of the list is 0, it means the list is empty. e
Example:
my_list = []
if len(my_list) == 0:
print("The list is empty")
else:
print("The list is not empty")
This method does not work is the list can be None
- if the list is None
it will raise a TypeError: object of type 'NoneType' has no len()
This can be handled by adding an additional check for None
in the if
clause (if my_list is None or len(my_list) == 0
) or wrapping the if
statement in a try-except
try:
# Assuming my_list is defined elsewhere in your code
if my_list is None or len(my_list) == 0:
print("The list is either None or empty")
else:
print("The list is not None and not empty")
except NameError:
print("The list does not exist")
Comparison to an empty list
You can directly compare the list to an empty list.
my_list = []
if my_list == []:
print("The list is empty")
else:
print("The list is not empty")
This will not work if the list is None
- if None
the comparison is false - so you will get the ‘The list is not empty’ message.
While technically true, this might not be the result that you need or expect. Add an addtional check to the if
clause - if my_list == [] or my_list is None
Using bool()
You can use the bool()
function to convert the list to a boolean value, bool(mylist)
will be False if the list is empty
my_list = []
if bool(my_list):
print("The list is not empty")
else:
print("The list is empty")
Using any()
The any()
function returns True
if at least one element in the iterable is true. In the case of an empty list, it returns False
.
my_list = []
if not any(my_list):
print("The list is empty")
else:
print("The list is not empty")
This will not work if the list is None
, it will generate a TypeError: 'NoneType' object is not iterable
Use an additional check in the if
or wrap it in a try-except
clause (see example above)
Other articles