How to Check if a Python List Is Empty

Mateen Kiani

Mateen Kiani

Published on Wed Aug 06 2025·4 min read

how-to-check-if-a-python-list-is-empty

Introduction

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.

Why Check for Empty Lists?

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:

  • Provide default values or fallback logic.
  • Avoid IndexError when accessing elements.
  • Short-circuit loops or processing when there’s nothing to do.

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.

Method 1: Using len()

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:

  • Explicit and immediately clear to readers.
  • No hidden behavior; you’re directly querying the list’s size.

Cons:

  • Slightly more verbose than other options.

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.

Method 2: Direct Truthiness Check

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:

  • Empty sequences evaluate to False in boolean contexts.
  • Non-empty sequences evaluate to True.

Pros:

  • Very concise and idiomatic in Python.
  • Preferred in many style guides for readability.

Cons:

  • May confuse newcomers unfamiliar with Python’s truthiness rules.

Tip: When you see if my_list: in code, remember it’s checking for “not empty.”

Method 3: Comparing to []

Another option is a direct comparison:

if my_list == []:
print("Completely empty")

Pros:

  • Clear intent: you want exactly an empty list.

Cons:

  • Less flexible if my_list could be another sequence type.
  • Performs an equality check, which may be marginally slower than a boolean test.

Use this if you strictly want to ensure the variable is exactly an empty list and not a subclass or different sequence type.

Common Pitfalls

  1. 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.
  2. Mutable Defaults: Don’t set default function parameters to []: python def foo(items=[]): # avoid this if not items: items = [] # ... That default list persists across calls.
  3. Indexing without check: Accessing my_list[0] on an empty list raises IndexError. Always ensure non-empty first.

Performance Considerations

All methods here run in O(1) time because:

  • len() stores the size in the object header.
  • Boolean checks use the stored length.
  • Equality to [] is also optimized for empty lists.

So choose based on readability and team conventions rather than speed.

Best Practices Summary

  • Use if not my_list: for concise, Pythonic checks.
  • Use len(my_list) == 0 when you want explicit length checks.
  • Avoid default mutable arguments.
  • Always guard any direct indexing or popping with an emptiness check.

Conclusion

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!


Mateen Kiani
Mateen Kiani
kiani.mateen012@gmail.com
I am a passionate Full stack developer with around 4 years of experience in MERN stack development and 1 year experience in blockchain application development. I have completed several projects in MERN stack, Nextjs and blockchain, including some NFT marketplaces. I have vast experience in Node js, Express, React and Redux.