Question
In Python, if I have an empty list like this:
a = []
How can I check whether a is empty?
Short Answer
By the end of this page, you will understand the simplest and most Pythonic way to check whether a list is empty, why it works, when to use alternatives like len(), and which patterns to avoid.
Concept
In Python, lists have truth value behavior.
An empty list is considered falsey, and a non-empty list is considered truthy. That means you can check whether a list has any items by using it directly in a condition.
items = []
if not items:
print("The list is empty")
This is the most common and idiomatic Python approach.
Why this matters:
- It makes code shorter and easier to read.
- It works consistently with other empty collections too, such as strings, tuples, sets, and dictionaries.
- It is the style most Python developers expect to see in real code.
Common truthy/falsey examples in Python:
[]→ falsey[1, 2]→ truthy""→ falsey"hello"→ truthy{}→ falseyNone→ falsey
So when you want to know whether a list is empty, you usually do not compare it to [] directly and you usually do not need len() unless that communicates your intent better in a specific situation.
Mental Model
Think of a list like a bag.
- If the bag contains nothing, Python treats it like no.
- If the bag contains at least one item, Python treats it like yes.
So this condition:
if not a:
means:
- "If the bag has nothing in it..."
And this:
if a:
means:
- "If the bag has something in it..."
This mental model helps because Python often lets collections answer the question: Do you contain anything? without extra code.
Syntax and Examples
The most Pythonic syntax is:
if not a:
print("empty")
Example 1: Empty list
a = []
if not a:
print("List is empty")
else:
print("List is not empty")
Output:
List is empty
Example 2: Non-empty list
a = [10, 20, 30]
if not a:
print("List is empty")
else:
print("List is not empty")
Output:
List is not empty
Example 3: Using len()
Step by Step Execution
Consider this code:
a = []
if not a:
print("empty")
else:
print("not empty")
Step-by-step
-
a = []- A variable named
ais created. - It refers to an empty list.
- A variable named
-
if not a:- Python evaluates
ain a boolean context. - Because
ais an empty list, it is falsey. not abecomesTrue.
- Python evaluates
-
print("empty")- Since the condition is
True, this line runs.
- Since the condition is
-
else:block is skipped- Python does not execute the
elseblock because the block already matched.
- Python does not execute the
Real World Use Cases
Checking whether a list is empty appears constantly in real programs.
API results
users = []
if not users:
print("No users found")
Useful when an API returns no records.
Search results
matches = search_products("laptop")
if not matches:
print("No products matched your search")
Useful in web apps and command-line tools.
File processing
lines = []
if not lines:
print("The file had no data")
Useful when reading files, CSV rows, or logs.
Validation before processing
tasks = []
if not tasks:
print("Nothing to process")
else:
for task in tasks:
print(task)
Useful when avoiding unnecessary loops or expensive work.
Real Codebase Usage
In real projects, developers often use empty-list checks as part of control flow and validation.
Guard clauses
A guard clause exits early when there is nothing to do.
def process_orders(orders):
if not orders:
return "No orders to process"
for order in orders:
print(order)
This keeps the main logic less nested.
Validation
def send_emails(recipients):
if not recipients:
raise ValueError("Recipient list cannot be empty")
This prevents invalid operations.
Conditional rendering or messaging
def show_notifications(messages):
if not messages:
return "No new notifications"
return f"You have {(messages)} notifications"
Common Mistakes
Mistake 1: Comparing to None when you mean empty
Broken idea:
a = []
if a is None:
print("empty")
Why it is wrong:
Nonemeans no value.[]means a real list with zero items.- These are not the same thing.
Correct:
if not a:
print("empty")
Mistake 2: Using is []
Broken code:
a = []
if a is []:
print("empty")
Why it is wrong:
ischecks object identity, not value.aand[]are different list objects.
Comparisons
| Approach | Example | Works? | Notes |
|---|---|---|---|
| Truthiness check | if not a: | Yes | Most Pythonic and preferred |
| Length check | if len(a) == 0: | Yes | Clear, but more verbose |
| Equality check | if a == []: | Yes | Works, but less idiomatic |
| Identity check | if a is []: | No | Incorrect for checking emptiness |
| None check | if a is None: | Only for None |
Cheat Sheet
# Preferred empty check
if not a:
print("empty")
# Preferred non-empty check
if a:
print("has items")
# Also works, but less idiomatic
if len(a) == 0:
print("empty")
# Works, but less preferred
if a == []:
print("empty")
Rules
- Empty lists are falsey.
- Non-empty lists are truthy.
- Use
not ato check for empty. - Use
ato check for non-empty. - Do not use
is []. - Use
is Noneonly when checking forNone.
Important edge case
if not a:
This is True for both:
FAQ
What is the Pythonic way to check if a list is empty?
Use:
if not a:
This is the standard and most idiomatic approach in Python.
Is len(a) == 0 wrong in Python?
No. It works correctly. It is just more verbose than if not a:.
Should I use a == [] to check for an empty list?
It works, but most Python developers prefer if not a: because it is simpler and more readable.
Why does if not a work for lists?
Because Python treats empty collections as falsey and non-empty collections as truthy.
What is the difference between [] and None?
[]means an empty list exists.Nonemeans there is no value.
They are different and should be checked differently when needed.
Why is a is [] incorrect?
Because is checks whether two variables point to the exact same object, not whether they have the same value.
Mini Project
Description
Build a small Python function that checks a list of tasks before processing them. This demonstrates how empty-list checks are used in real programs to avoid unnecessary work and return helpful messages.
Goal
Create a function that reports whether there are tasks to process and prints each task only when the list is not empty.
Requirements
- Write a function that accepts a list of tasks
- Return a message if the list is empty
- Print each task if the list has items
- Test the function with both an empty list and a non-empty list
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.
Convert Bytes to String in Python 3
Learn how to convert bytes to str in Python 3 using decode(), text mode, and proper encodings with practical examples.