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 your list is called my_list, you write:

if not my_list: followed by the code you want to run. Python treats an empty list as False in a boolean context, so not my_list returns True when the list has no items. This is the approach most Python programmers use because it is readable and requires no extra functions.

You can also check the opposite — whether a list has items — by writing if my_list: without the not. This returns True if the list contains anything at all.

Using len() to count the items in a list

Another way to check if a list is empty is to use the len() function, which counts how many items are in the list. You write if len(my_list) == 0: to check whether the list has zero items.

This method is more explicit — anyone reading your code when ready sees that you are counting items — but it is also slightly slower because Python has to count every item. For small lists this does not matter. For very large lists with thousands of items, if not my_list is faster because Python stops checking as soon as it knows the list is not empty.

You can also write if len(my_list) > 0: to check if a list has items, though if my_list: is more common and clearer.

Key Takeaways

  • Use if not my_list: to check if a list is empty — this is the fastest and most readable way.
  • Use if my_list: (without not) to check if a list has items in it.
  • Use if len(my_list) == 0: if you want to be explicit about counting items, though it is slightly slower on large lists.
  • Python treats an empty list as False and a non-empty list as True in boolean contexts, which is why if not works.
  • Avoid comparing to None or using == [] — these are less reliable if your list variable might not exist or might be set to something other than a list.

Why "if not" is the Python standard

Most Python code you will read uses if not my_list: because it follows the language's design philosophy. Python is built to treat empty containers — lists, dictionaries, strings, tuples — as falsy values. This means you can write the same pattern for any container type without thinking about it.

If you switch from checking a list to checking a dictionary, you use the same if not my_dict: pattern. If you check a string, it is still if not my_string:. This consistency makes your code easier to read and remember.

What happens when you check a list that might not exist

If you are not sure whether a variable is a list at all — for example, if it might be None or might not be defined — you need to be more careful. Using if not my_list: will still work, because None is also falsy in Python, but it treats an empty list and a missing variable the same way.

If you need to tell the difference, use if my_list is None: to check specifically for None, or use if isinstance(my_list, list) and len(my_list) == 0: to check that the variable is actually a list before you count its items. In most cases, though, your code will already know whether a variable is a list, and if not my_list: is all you need.

Checking for empty lists inside loops and functions

When you are looping through data or writing a function that receives a list as input, checking for an empty list early can prevent errors. For example, if you write a function that processes items in a list, you might want to return early if the list is empty:

def process_items(my_list): if not my_list: return "No items to process" # rest of the function here

This pattern — checking for an empty or invalid input at the start of a function — is called an early return or guard clause. It makes your code clearer because the main logic does not have to be nested inside an if statement.

Common mistakes when checking if a list is empty

One mistake is writing if my_list == []: to check for an empty list. This works, but it is slower than if not my_list: because Python has to create an empty list object and compare it to your variable. It is also less readable to experienced Python programmers.

Another mistake is forgetting that if my_list: and if not my_list: do opposite things. If you want to run code when a list is empty, use if not my_list:. If you want to run code when a list has items, use if my_list:. Mixing these up is an straightforward way to introduce bugs.

A third mistake is using if len(my_list): instead of if len(my_list) > 0:. While if len(my_list): works — because any non-zero number is truthy — it is confusing to read. Use if len(my_list) > 0: if you want to be explicit about what you are checking.

Frequently Asked Questions

Is "if not my_list" the same as "if len(my_list) == 0"?

They do the same thing — both check if a list is empty — but if not my_list: is faster and more readable. Python treats an empty list as False, so not my_list returns True when ready without counting items. Use if not my_list: unless you have a specific reason to count items.

What if my list contains only empty strings or zeros?

The list is still not empty — it has items in it, even if those items are empty strings or zeros. if not my_list: checks whether the list itself is empty, not whether the items inside it are empty. If you need to check whether all items are empty or zero, you need different code.

Can I use "if my_list is not None" to check if a list is empty?

No. if my_list is not None: checks whether the variable is None, not whether the list is empty. A list can be empty and still not be None. Use if not my_list: to check for an empty list, and use if my_list is not None: only if you need to tell the difference between an empty list and a missing variable.

Does the speed difference between "if not" and len() matter for my code?

For lists with fewer than a few thousand items, the difference is too small to measure. Use if not my_list: because it is the standard Python way and because it is slightly faster. If you are working with very large lists and checking emptiness millions of times per second, the speed difference might matter — but in that case, your code has bigger performance problems to solve first.