Python provides several ways to check if a list is empty or not. This tutorial will guide you through the different methods to check if a list in empty in Python. The methods we’ll explore include:
- The len() function
- The “not” operator
- Comparing the list to an empty list
- The all() function
- The any() function
Check if a List is Empty in Python
Here are the 5 different ways to check if a list is empty in Python with examples.
1. Using the len() Function
The len() function is a built-in Python function that returns the number of items in an object. When it’s applied to a list, it returns the number of elements in the list. If a list is empty, the len() function will return 0.
Here’s how you use the len() function to check if a list is empty:
my_list = []
if len(my_list) == 0:
print("The list is empty.")
else:
print("The list is not empty.")
You can see the output here:
Here’s an example of checking a non-empty list using the len()
function:
my_list = [1, 2, 3]
if len(my_list) == 0:
print("The list is empty.")
else:
print("The list is not empty.")
Output:
The list is not empty.
2. Using the “not” Operator
The “not” operator in Python returns True if the operand (the value it’s operating on) is False. In the context of lists, an empty list is considered False when converted to a Boolean context, while a non-empty list is considered True.
Here’s how you can use the “not” operator to check if a list is empty:
my_list = []
if not my_list:
print("The list is empty.")
else:
print("The list is not empty.")
Here’s how you use the “not” operator to check a non-empty list:
my_list = [1, 2, 3]
if not my_list:
print("The list is empty.")
else:
print("The list is not empty.")
Here the output will come as:
The list is not empty.
3. Comparing the List to an Empty List
You can also check if a list is empty by comparing it directly to an empty list ([]). This method might be less readable than the others if you’re new to Python, but it’s still effective.
Here’s how you do it:
my_list = []
if my_list == []:
print("The list is empty.")
else:
print("The list is not empty.")
Here’s how you check a non-empty list by comparing it to an empty list:
my_list = [1, 2, 3]
if my_list == []:
print("The list is empty.")
else:
print("The list is not empty.")
You can see the output:
4. Using the all() Function
The all() function returns True if all elements in the list are True. An empty list doesn’t have any elements, so all() returns True for an empty list.
However, be careful when using this method. If your list includes any elements that Python considers False (like 0, False, or an empty string), all() will return False, even if the list is not empty.
Here’s an example:
my_list = []
if all(my_list):
print("The list is empty.")
else:
print("The list is not empty.")
You can see the output:
Here’s an example of checking a non-empty list using the all()
function:
my_list = [1, 2, 3]
if all(my_list):
print("The list is empty.")
else:
print("The list is not empty.")
Output:
The list is not empty.
Note: The all()
function returns True
if all elements in the iterable are True
. So if the list contains any ‘falsy’ values (like 0, False, None or an empty string), all()
will return False
.
5. Using the any() Function
The any() function returns True if at least one element in the list is True. An empty list doesn’t have any elements, so any() returns False for an empty list.
Like with all(), be aware that this method considers the truthiness of the list’s elements. If your list includes elements that Python considers False, any() might not work as you expect.
Here’s how you use the any() function:
my_list = []
if not any(my_list):
print("The list is empty.")
else:
print("The list is not empty.")
Here’s an example of checking a non-empty list using the any()
function:
my_list = [1, 2, 3]
if not any(my_list):
print("The list is empty.")
else:
print("The list is not empty.")
Output:
The list is not empty.
Note: The any()
function returns True
if at least one element in the iterable is True
. So even if there are ‘falsy’ values in the list, as long as there’s at least one ‘truthy’ value, any()
will return True
.
Conclusion
These are some of the most common ways to check if a list is empty in Python. They all have their use cases, so choose the one that best suits your needs. Understanding how to properly check for empty lists is essential for effective data handling and control flow in Python.
You may also like:
- create a list of floats in Python
- Python list methods with examples
- How to Create a String from a List in Python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.