The simplest way to check if a list is empty

The most direct way to check if a list is empty in Python 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 test a list directly in an if statement without any extra functions or methods.

Here is the basic pattern:

if not my_list: will execute if the list is empty. if my_list: will execute if the list has at least one item. This works because Python treats an empty list as falsy and a non-empty list as truthy.

This approach is preferred by most Python developers because it is concise, readable, and requires no function calls. It works the same way for other empty containers like dictionaries, tuples, and strings.

Key Takeaways

  • Use if not my_list: to check if a list is empty; this is the most Pythonic and readable approach.
  • You can also compare the list length to zero with if len(my_list) == 0:, though this is less common and slightly slower.
  • Comparing a list to an empty list with if my_list == []: works but is considered less idiomatic than checking truthiness.
  • Empty lists are falsy in Python, so they behave like False in boolean contexts, while non-empty lists are truthy.

Using len() to check list length

Another way to check if a list is empty is to use the len() function, which returns the number of items in the list. If len(my_list) == 0, the list is empty. If the length is greater than zero, the list contains items.

This approach is explicit and clear about what you are checking, which can make code easier to understand at first glance. However, it is slightly less efficient than checking truthiness because Python has to count the items, even though it already knows the list is empty.

You might see this pattern in older code or in situations where you want to be very explicit about what you are testing. For most cases, checking truthiness directly is preferred.

Comparing a list to an empty list

You can also check if a list is empty by comparing it directly to an empty list using if my_list == []:. This comparison returns True if the list is empty and False if it contains any items.

While this method works correctly, it is generally considered less Pythonic than the other approaches. It is also slightly less efficient because Python has to create an empty list object for the comparison, then check if the two lists are equal.

This pattern appears in some codebases, but most Python style guides recommend using truthiness checking instead, as it is more concise and idiomatic.

Performance differences between methods

The truthiness check (if not my_list:) is the fastest method because Python already knows whether a list is empty without doing any extra work. The len() function is slightly slower because it has to compute or retrieve the list length, though the difference is negligible in most real-world code.

Comparing to an empty list (if my_list == []:) is the slowest of the three methods because it creates a new empty list object and then compares the two lists element by element. Again, the difference is small enough that it does not matter in typical programs.

For production code where performance matters, use truthiness checking. For code where clarity is more important than speed, any of these methods will work fine.

Checking for empty lists in loops and functions

When you are working with lists inside loops or functions, you often need to handle the empty list case separately. The truthiness approach works well here too. For example, you might skip processing if a list is empty, or provide a default value.

In a function that returns a list, you might check if not result: before returning it, or handle the empty case inside the function logic. In a loop, you might use if not items: to break early or skip to the next iteration.

The truthiness check integrates naturally into these patterns because it reads like English: "if not my_list" means "if my list is empty".

Common mistakes when checking for empty lists

One mistake is checking if my_list: when you meant to check if not my_list:. The first one executes when the list has items; the second executes when it is empty. Reversing the logic is easy to do and can cause bugs that are hard to spot.

Another mistake is assuming that None and an empty list are the same thing. If a variable might be None instead of a list, you need to check for that separately. if my_list: will be False for both None and an empty list, but they are different states. Use if my_list is not None and my_list: if you need to distinguish between them.

A third mistake is using if len(my_list) > 0: instead of if my_list:. Both work, but the second is more readable and more Pythonic.

Frequently Asked Questions

What is the difference between an empty list and None?

An empty list is a list object with zero items: []. None is a special Python value that means "no value at all". An empty list is still a list; None is not a list. If a variable might be either, check for None first: if my_list is None: or if my_list is not None and my_list:.

Does checking truthiness work for other data types like dictionaries and tuples?

Yes. Empty dictionaries, tuples, strings, and sets all evaluate to False in a boolean context. Non-empty ones evaluate to True. So if not my_dict: works the same way as if not my_list:.

Is it faster to check if a list is empty or to check its length?

Checking truthiness is slightly faster because Python does not have to compute anything. Checking len() is marginally slower. In practice, the difference is so small that it does not matter unless you are checking millions of lists in a tight loop.

Can I use if not my_list if the list might contain False or 0?

Yes. The truthiness check only cares whether the list itself is empty, not what values are inside it. A list containing [0, False, None] is not empty and will evaluate to True.

What happens if I check if a list is empty before it is defined?

You will get a NameError because the variable does not exist. Always make sure the variable is defined before you check it. If it might not be defined, use a try-except block or check with if 'my_list' in locals(): first.