How to Check if a List Is Empty in Python
What you’ll build or solve
You’ll check if a Python list is empty and handle both cases cleanly.
When this approach works best
Checking if a list is empty works well when you:
Learn Python on Mimo
- Guard before accessing items like
items[0]oritems[-1]. - Decide what to display, like showing “No results” when a search returns nothing.
- Skip work early when there’s nothing to process.
Skip this approach when you only want to loop. A for item in items loop already does nothing for an empty list.
Prerequisites
- Python 3 installed
- You know what a list is
Step-by-step instructions
1) Use the list directly in a condition
This is the standard Python way. Empty lists are falsy, non-empty lists are truthy.
Python
items = []
if items:
print("Has items")
else:
print("Empty")
What to look for: if items: checks the list itself, not the values inside it. A list like [0, 0] is still not empty.
2) Use len() when you need a number
Use len() when your logic depends on the count, not just empty vs not empty.
Python
items = ["a", "b", "c"]
if len(items) == 0:
print("Empty")
else:
print(f"Count: {len(items)}")
What to look for: len(items) returns an integer, so you can compare it to thresholds like < 3 or >= limit.
Examples you can copy
Example 1: Guard before indexing
Python
results = []
if results:
first = results[0]
else:
first = None
print(first)
Example 2: Print a message when there are no matches
Python
matches = ["file1.txt", "file2.txt"]
if not matches:
print("No matches found")
else:
for m in matches:
print(m)
Example 3: Loop safely without an explicit check
Sometimes the cleanest option is to just loop.
Python
items = []
for x in items:
print(x)
print("Done")
Example 4: Provide a default value when the list is empty
Python
scores = []
top_score = max(scores) if scores else 0
print(top_score)
Example 5: Validate before doing a pairwise operation
Python
names = ["Amina", "Luka"]
scores = []
if not names or not scores:
print("Missing data")
else:
pairs = list(zip(names, scores))
print(pairs)
Example 6: Use early returns in functions
In functions, checking at the top and returning early keeps the main logic cleaner.
Python
def average(nums):
if not nums:
return None
return sum(nums) / len(nums)
print(average([]))
print(average([10, 20, 30]))
Common mistakes and how to fix them
Mistake 1: Comparing to [] everywhere
What you might do:
Python
items = []
if items == []:
print("Empty")
Why it’s not ideal: it works, but it’s less idiomatic and awkward if the value might be None.
Correct approach:
Python
items = []
if not items:
print("Empty")
If the value might be None, handle that intentionally:
Python
items = None
if not items:
print("Empty or None")
Mistake 2: Checking the wrong thing
What you might do:
Python
items = [0, 0, 0]
if not items[0]:
print("Empty")
Why it breaks: this checks the first element, not the list. A non-empty list can still contain falsy values like 0 or "".
Correct approach:
Python
items = [0, 0, 0]
if not items:
print("Empty")
else:
print("Not empty")
Mistake 3: Calling len() on something that isn’t a list
What you might do:
Python
items = None
print(len(items))
Why it breaks: None has no length, so Python raises TypeError.
Correct approach: decide how you want to treat None.
Python
items = None
if not items:
print("Empty or None")
Or be strict:
Python
items = None
if items is None:
raise ValueError("items must be a list")
Troubleshooting
If you see IndexError: list index out of range, you indexed into an empty list. Add a guard like if items: before items[0].
If if items: behaves unexpectedly, confirm items is actually a list. Print type(items).
If you see TypeError: object of type ... has no len(), you called len() on the wrong type, often None.
If your loop never runs, the list might be empty. Print the list or its length to confirm.
Quick recap
- Use
if items:for the standard empty-list check. - Use
len(items) == 0when you need a numeric count check. - Guard before indexing with
items[0]oritems[-1]. - You usually don’t need a check before
for item in items. - Decide how you want to handle
Noneif it can appear instead of a list.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot