The simplest way to check if a list is empty
In Python, the most direct way to check if a list is empty is to use the truthiness of the list itself. An empty list evaluates to False in a boolean context, while any list with items evaluates to True. This means you can write:
if not my_list: to check if the list is empty, or if my_list: to check if it has items. This works because Python treats empty containers as falsy values by default, without needing to count the items or call a function.
This approach is faster than checking the length and is the standard Python idiom. Most experienced Python developers recognize it immediately, so your code will be easier for others to read.
Key Takeaways
- Use if not my_list: to check if a list is empty — this is the fastest and most Pythonic approach.
- You can also use if len(my_list) == 0: to check the length explicitly, though this is slower and less common.
- Comparing a list to an empty list with if my_list == []: works but is considered less readable than checking truthiness.
- Empty lists are falsy values in Python, so they automatically evaluate to False in conditional statements without any extra code.
Using truthiness: the recommended method
When you write if not my_list:, Python evaluates the list as a boolean. Since empty lists are falsy, the condition is True when the list has no items. This is the method recommended in PEP 8, Python's official style guide, because it is concise and efficient.
Here is how it looks in practice:
my_list = [] if not my_list: print("The list is empty") else: print("The list has items")
The reverse also works: if my_list: checks if the list has at least one item. You do not need to know how many items are in the list — Python stops checking as soon as it finds one.
Checking the length explicitly
You can also check the length of a list using the len() function. This approach is more explicit about what you are checking, which some developers prefer for clarity:
if len(my_list) == 0: print("The list is empty")
This method works reliably and is easy to understand at a glance. However, it is slightly slower than checking truthiness because Python has to count the items in the list, even though it stops as soon as it finds one. For most programs, the difference is negligible, but in performance-critical code, truthiness checking is preferable.
You can also check if a list is not empty using if len(my_list) != 0:, though if my_list: is more idiomatic.
Comparing to an empty list
Another way to check if a list is empty is to compare it directly to an empty list:
if my_list == []: print("The list is empty")
This approach is clear and works correctly, but it is considered less Pythonic than truthiness checking. It also requires Python to create a temporary empty list for the comparison, which adds a small overhead. Most Python style guides recommend avoiding this method in favor of the truthiness approach.
Performance differences between methods
If you are working with very large lists or checking emptiness millions of times in a loop, the performance difference matters. Truthiness checking is the fastest because Python does not need to count items or create temporary objects — it simply checks whether the list has a length greater than zero.
The len() function is nearly as fast for most practical purposes, since Python stores the list length internally and does not have to count items each time. Comparing to an empty list is slightly slower because it creates a new list object and compares the two.
For typical programs, all three methods are fast enough that you should choose based on readability rather than performance. Use truthiness checking because it is the standard Python convention.
Checking emptiness in different contexts
Empty lists behave consistently across different Python contexts. In a while loop, an empty list is falsy:
while my_list: item = my_list.pop() print(item)
This loop runs as long as the list has items and stops when the list becomes empty. You can also use truthiness in list comprehensions, conditional expressions, and function arguments. The behavior is the same everywhere: empty lists are falsy, non-empty lists are truthy.
If you are checking whether a list contains a specific item, use the in operator instead: if item in my_list:. This is different from checking if the list itself is empty.
Frequently Asked Questions
Is checking truthiness faster than using len()?
Truthiness checking is marginally faster because Python does not need to retrieve the length value. However, the difference is negligible for most programs. Use truthiness because it is the Python convention, not primarily for performance.
What happens if I check an undefined variable?
If you try to check a variable that does not exist, Python raises a NameError. Make sure the list is defined before you check it. If the list might not exist, use if 'my_list' in locals(): to check whether the variable exists first.
Can I check if a list is empty inside a function?
Yes, the same methods work inside functions. You can check truthiness, use len(), or compare to an empty list. The behavior is identical whether you are at the top level of your program or inside a function.
Does checking emptiness work the same way for tuples and dictionaries?
Yes, all Python containers — lists, tuples, dictionaries, sets, and strings — are falsy when empty and truthy when they contain items. You can use if not my_dict: or if not my_tuple: with the same results.
What if the list contains only None or False values?
The list is still truthy because it contains items, even if those items are falsy values. Truthiness checks whether the list itself is empty, not whether its contents are truthy. A list with [None, False, 0] is non-empty and evaluates to True.