Last modified: Sep 17, 2023 By Alexander Williams
Check if List is Empty in Python (4 Methods - Examples)
Lists are one of the most fundamental data structures in Python. They are used to store collections of data in a sequential order. Lists can be empty or contain one or more elements.
In some cases, you may need to check if a list is empty before operating on it. For example, you may want to avoid trying to iterate over an empty list or appending an element to an empty list.
In this article, you will be introduced to 4 methods, including examples, that can be used to determine whether a list is empty in Python.
Method 1: Using the not Keyword
The not
keyword is a commonly used and Pythonic way to check for empty lists
my_list = []
print(not my_list)
Output:
True
my_list = []
print(not my_list)
Output:
False
As you can observe, the not
keyword returns True
when the list is empty. In any other case, it will return False
.
Let's explore how to use the not
keyword 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")
Method 2: Using the len() Function
len()
returns the number of elements in a list. When the list is empty, it will return 0.
Here is an example of how to use len() to check if the list is empty:
# Empty list
my_list = []
# Check if list is empty
if len(my_list) == 0:
print("The list is empty")
else:
print("The list is not empty")
Output:
The list is empty
Method 3: Using a Ternary Conditional Expression
For those who prefer a more compact style, a ternary conditional expression is another option.
my_list = []
is_empty = True if not my_list else False
if is_empty:
print("The list is empty")
else:
print("The list is not empty")
Method 4: Using the == Operator
While not as concise, you can also check for an empty list by comparing it directly to an empty list using the ==
operator.
The ==
operator is used for equality comparison. It is used to check if two objects have the same content or value.
my_list = []
if my_list == []:
print("The list is empty")
else:
print("The list is not empty")
Comparing Methods Pros and Cons
here's a table that summarizes the positive and negative aspects of each method for checking if a list is empty in Python:
Method | Positive Aspects | Negative Aspects |
---|---|---|
Using len() function |
- Straightforward and widely used. - Works for all iterable types, not just lists. |
- Requires more typing compared to - Slightly less Pythonic. |
Using the not keyword |
- Concise and Pythonic. - Works for all iterable types. - Easy to read and understand. |
- May not be immediately obvious to beginners. - Limited to checking if it's "falsy." |
Using a ternary conditional expression |
- Provides a compact one-liner option. - Allows for custom variable naming. |
- Can be less readable if not used judiciously. - Requires additional variable. |
Using the == operator |
- Explicitly checks for an empty list. - Clear and understandable. |
- Slightly less concise. - May not work as expected with custom classes. |
Conclusion
We've explored various methods to accomplish this task, from using he not
keyword function and the len()
function to employing ternary conditional expressions and the ==
operator.
Whether you prioritize readability or brevity in your code, there's a method that suits your style.