Mateen Kiani
Published on Wed Aug 06 2025·4 min read
Checking whether a list is empty may seem trivial, but for developers it’s a vital guardrail against unexpected bugs and crashes. While many of us default to checking len(my_list) == 0
, there are subtleties and more Pythonic approaches that often go unnoticed. What happens under the hood when you use direct truthiness checks, and which method is the clearest for your teammates to read?
In this guide, we'll explore several ways to determine if a list is empty, from the classic len()
function to direct boolean checks and comparisons. Understanding these techniques can help you write more readable code, prevent off-by-one errors, and make maintenance easier. By the end, you'll know which approach suits various scenarios and have tips to avoid common pitfalls.
An empty list is not just “nothing” – it’s a container with zero elements. If your function assumes at least one item, operations like indexing, popping, or iterating can raise exceptions. Proactively checking for emptiness allows you to:
IndexError
when accessing elements.By placing a clear check at the start, you free the rest of your code from handling edge cases, leading to cleaner functions and fewer try/except blocks downstream.
The most straightforward way is using the built-in len()
function:
my_list = []if len(my_list) == 0:print("List is empty")else:print("List has items")
Pros:
Cons:
Tip: If you need more list utilities—like finding the last element or slicing—you might want to revisit how you determine list length later on. Check out this guide on length of array for deeper insights.
In Python, containers like lists have a boolean value: False when empty, True otherwise. You can leverage this for concise code:
if not my_list:print("No items here")else:print("We have items!")
Why it works:
False
in boolean contexts.True
.Pros:
Cons:
Tip: When you see
if my_list:
in code, remember it’s checking for “not empty.”
[]
Another option is a direct comparison:
if my_list == []:print("Completely empty")
Pros:
Cons:
my_list
could be another sequence type.Use this if you strictly want to ensure the variable is exactly an empty list and not a subclass or different sequence type.
None
vs []
: Remember, None
is not the same as an empty list. Checking if not my_list:
will treat None
the same as []
, which might mask bugs.[]
:
python
def foo(items=[]): # avoid this
if not items:
items = []
# ...
That default list persists across calls.my_list[0]
on an empty list raises IndexError
. Always ensure non-empty first.All methods here run in O(1) time because:
len()
stores the size in the object header.[]
is also optimized for empty lists.So choose based on readability and team conventions rather than speed.
if not my_list:
for concise, Pythonic checks.len(my_list) == 0
when you want explicit length checks.Checking if a list is empty is a small but crucial tool in your Python toolkit. Whether you choose len()
for clarity or a direct truthiness check for brevity, the goal is to make your intentions clear and avoid runtime surprises. By following these guidelines—watching out for None
, mutable defaults, and off-by-one errors—you’ll write safer and more maintainable code. Next time you see an empty container, you’ll know exactly how to guard it properly!