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.
Bash
items= []
ifitems:
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"]
iflen(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
results= []
ifresults:
first=results[0]
else:
first=None
print(first)
Example 2: Print a message when there are no matches
matches= ["file1.txt","file2.txt"]
ifnotmatches:
print("No matches found")
else:
forminmatches:
print(m)
Example 3: Loop safely without an explicit check
Sometimes the cleanest option is to just loop.
CSS
items= []
forxinitems:
print(x)
print("Done")
Example 4: Provide a default value when the list is empty
scores= []
top_score=max(scores)ifscoreselse0
print(top_score)
Example 5: Validate before doing a pairwise operation
CSS
names= ["Amina","Luka"]
scores= []
ifnotnamesornotscores:
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.
defaverage(nums):
ifnotnums:
returnNone
returnsum(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:
CSS
items= []
ifitems== []:
print("Empty")
Why it’s not ideal: it works, but it’s less idiomatic and awkward if the value might be None.
Correct approach:
CSS
items= []
ifnotitems:
print("Empty")
If the value might be None, handle that intentionally:
CSS
items=None
ifnotitems:
print("Empty or None")
Mistake 2: Checking the wrong thing
What you might do:
CSS
items= [0,0,0]
ifnotitems[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:
Bash
items= [0,0,0]
ifnotitems:
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.
CSS
items=None
ifnotitems:
print("Empty or None")
Or be strict:
CSS
items=None
ifitemsisNone:
raiseValueError("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