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 if not statement. If you have a list called my_list, you can write:
if not my_list: will return True if the list contains no items, and False if it contains at least one item. This works because Python treats an empty list as a "falsy" value — a value that evaluates to false in a boolean context. A list with any items in it is "truthy".
You can also check the opposite direction: if my_list: will return True only if the list has items. Both approaches are equally valid; choose whichever reads more naturally for what you are trying to do.
Key Takeaways
- Use if not my_list: to check whether a list is empty; this is the most readable and Pythonic approach.
- An empty list is falsy in Python, so it evaluates to False in a boolean context without any extra function calls.
- You can also use if len(my_list) == 0: to check the length, but this is slower and less idiomatic than the falsy check.
- The not operator flips the boolean value, so if not my_list: means "if the list is falsy" or "if the list is empty".
Why the falsy check is faster than checking length
When you write if len(my_list) == 0:, Python has to count every item in the list to determine its length, then compare that number to zero. For a list with millions of items, this takes measurable time.
When you write if not my_list:, Python straightforward checks whether the list object itself is empty — it does not count anything. This check happens in constant time, meaning it takes the same amount of time whether the list has 10 items or 10 million items.
For small lists in a script you run once, the difference is invisible. For code that runs thousands of times per second in a web process or data processing pipeline, the falsy check can add up to real performance savings.
Using the falsy check in real code
Here is how you would use this pattern in a function that processes a list of user names:
def greet_users(names): if not names: print("No users to greet") return for name in names: print(f"Hello, {name}")
The function checks whether names is empty before trying to loop through it. If the list is empty, the function prints a message and exits early. If the list has items, the function greets each user.
This pattern — checking for an empty list and returning early — is called a "guard clause". It keeps your code readable by handling the empty case first, then writing the main logic without extra indentation.
When you might want to use len() instead
The len() function is occasionally clearer when you are checking for a specific number of items, not just whether the list is empty. For example, if you want to may support a list has exactly three items, if len(my_list) == 3: is more explicit than if my_list and len(my_list) == 3:.
You might also use len() if you are checking whether a list has at least a certain number of items: if len(my_list) >= 5: is clearer than building a boolean expression with not.
For the straightforward case of "is this list empty or not", the falsy check is always the right choice. It is faster, shorter, and every Python programmer expects to see it.
Common mistakes when checking for empty lists
One mistake is comparing a list to an empty list directly: if my_list == []:. This works, but it is slower than the falsy check because Python has to create an empty list object and then compare the two lists item by item. Stick with if not my_list: instead.
Another mistake is forgetting that an empty list is falsy. If you write if my_list: expecting it to return True when the list is empty, you will get the opposite of what you want. Remember: a list with items is truthy, and an empty list is falsy.
A third mistake is using if my_list is None: when you mean to check if the list is empty. These are different things. A list can be empty (zero items) or it can be None (not a list at all). If you need to handle both cases, write if not my_list: to catch empty lists, and if my_list is None: separately if you need to know whether the variable was never set.
How this works with other data types
The falsy check works the same way for other container types in Python. An empty string, empty dictionary, empty set, and empty tuple are all falsy. A string, dictionary, set, or tuple with at least one item is truthy.
This means you can use if not my_dict: to check if a dictionary is empty, if not my_string: to check if a string is empty, and so on. The pattern is consistent across all of Python's built-in container types, which makes your code easier to read and remember.
Frequently Asked Questions
Is if not my_list the same as if len(my_list) == 0?
They produce the same result, but if not my_list: is faster because it does not count the items. For most code, the difference is too small to notice. For performance-critical code that checks lists millions of times, the falsy check wins.
What if my_list is None instead of an empty list?
if not my_list: will return True for both an empty list and None. If you need to distinguish between them, check if my_list is None: first, or use if my_list and len(my_list) > 0: to catch only empty lists and skip None values.
Can I use this check inside a while loop?
Yes. while my_list: will loop as long as the list has items, and stop when the list becomes empty. This is useful when you are removing items from a list inside the loop and want to stop when nothing is left.
Does the order of items in the list matter for this check?
No. The falsy check only cares whether the list has any items at all, not what those items are or how they are ordered. A list with one item is truthy whether that item is at the beginning, middle, or end.