iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
To run code only when a Python list is empty, write if not my_list:. To run code only when it has items, write if my_list:. Python treats an empty list as false and a non-empty list as true, so the list itself can be the condition. You do not need to measure its length first.
The two standard checks
The idiomatic test uses the list directly as a condition:
my_list = []
if not my_list:
print("The list is empty")
if my_list:
print("The list has items")
Running this with an empty list prints only the first message. Adding one item to my_list reverses the result. Use if not my_list: when the empty case needs its own code, and if my_list: when the non-empty case does.
Why a list works as a condition
Python evaluates any object in a Boolean context. The Python Software Foundation’s built-in types reference explains that an object counts as true by default. It becomes false only when its __bool__() method returns False, or when its __len__() method returns zero. Lists and other built-in sequences fall into the second case, so an empty list is false and a list with elements is true. The rule is documented in Built-in Types — Python 3 documentation.
#1 Best Overall
The same logic applies to empty tuples, strings and dictionaries. It is a property of the object, not of the list syntax.
Why PEP 8 prefers the truth test over len()
PEP 8, Python’s official style guide, addresses this exact case. Its section on programming recommendations says: “For sequences, (strings, lists, tuples), use the fact that empty sequences are false.” It marks if not seq: and if seq: as correct, and marks if len(seq): and if not len(seq): as wrong. The guide is at PEP 8 – Style Guide for Python Code.
Rank #2
The comparison if len(my_list) == 0: still runs correctly. It is simply more explicit than the language requires, and it reads as more verbose than the truth test. If you are checking for emptiness and nothing else, the shorter form is the convention.
Free tools Windows power users keep installed
One-click scans. No signup required.
Comparing the common options
| Expression | What it tests | Guidance |
|---|---|---|
if not my_list: |
The list is empty | Recommended for the empty branch, per PEP 8 |
if my_list: |
The list has at least one element | Recommended for the non-empty branch, per PEP 8 |
if len(my_list) == 0: |
The length equals zero | Works, but more verbose than PEP 8 requires |
if my_list is None: |
The variable holds None |
Use when a missing value must be distinguished from an empty list |
Choose the expression by the condition your code actually needs: empty, non-empty, or missing.
When the value might be None
A truth test cannot tell None from an empty list. Both are false in a Boolean context, so if not items: runs the same branch for either. PEP 8 warns about this distinction. When your function may receive no argument at all, check for None first and then check for emptiness:
def report(items=None):
if items is None:
print("No list was supplied")
elif not items:
print("An empty list was supplied")
else:
print(f"{len(items)} items supplied")
Calling report() prints the first message, report([]) prints the second, and report([1, 2]) prints the third.
A list containing empty strings is not empty
The test looks only at the number of elements, not at whether each element is truthy. A list holding a single empty string has one element, so it is non-empty:
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallOutdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchnames = [""]
if names:
print("Non-empty")
This prints Non-empty. If you need to know whether every element has content, that is a separate check, such as all(names), and it answers a different question.
Quick Recap
Best Value
Quick checklist
- Use
if not my_list:for the empty case andif my_list:for the non-empty case. - Add
is Nonechecks before the emptiness test when a missing value is possible. - Do not expect a list containing empty strings to count as empty.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

