Question

How to Check if a List Is Empty in Python

pythonlist

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
  • {} → falsey
  • None → 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.

Take Quiz

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()

a = []

if len(a) == 0:
    print("List is empty")

This also works, but in Python, if not a: is usually preferred because it is shorter and idiomatic.

Example 4: Checking for non-empty

a = [1]

if a:
    print("The list has items")

This reads naturally: if the list exists and contains items, run the block.

Step by Step Execution

Consider this code:

a = []

if not a:
    print("empty")
else:
    print("not empty")

Step-by-step

  1. a = []

    • A variable named a is created.
    • It refers to an empty list.
  2. if not a:

    • Python evaluates a in a boolean context.
    • Because a is an empty list, it is falsey.
    • not a becomes True.
  3. print("empty")

    • Since the condition is True, this line runs.
  4. else: block is skipped

    • Python does not execute the else block because the if block already matched.

Now compare with:

a = [5]

if not a:
    print("empty")
else:
    print("not empty")

What changes?

  1. a contains one item.
  2. A non-empty list is truthy.
  3. not a becomes False.
  4. Python skips the if block.
  5. The else block runs and prints not empty.

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.

Take Quiz

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 {len(messages)} notifications"

Processing pipelines

def clean_numbers(numbers):
    if not numbers:
        return []
    return [n for n in numbers if n > 0]

Working with possibly missing values

Sometimes you may need to distinguish between None and an empty list:

def handle_items(items):
    if items is None:
        return "No list was provided"
    if not items:
        return "The list is empty"
    return "The list has data"

That distinction is common in APIs and function arguments.

Common Mistakes

Mistake 1: Comparing to None when you mean empty

Broken idea:

a = []

if a is None:
    print("empty")

Why it is wrong:

  • None means 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:

  • is checks object identity, not value.
  • a and [] are different list objects.

Correct:

if a == []:
    print("empty")

Or better:

if not a:
    print("empty")

Mistake 3: Overcomplicating with len()

This is valid but less idiomatic:

if len(a) == 0:
    print("empty")

Prefer:

if not a:
    print("empty")

Mistake 4: Forgetting the difference between empty and missing

def example(a=None):
    if not a:
        print("This prints for both None and []")

If you must distinguish them:

def example(a=None):
    if a is None:
        print("No list provided")
    elif not a:
        print("Empty list")

Mistake 5: Assuming this only works for lists

Truthiness checks also work for other collections:

if not "":
    print("Empty string")

if not {}:
    print("Empty dictionary")

Comparisons

ApproachExampleWorks?Notes
Truthiness checkif not a:YesMost Pythonic and preferred
Length checkif len(a) == 0:YesClear, but more verbose
Equality checkif a == []:YesWorks, but less idiomatic
Identity checkif a is []:NoIncorrect for checking emptiness
None checkif a is None:Only for NoneNot the same as an empty list

if not a vs if len(a) == 0

  • if not a: is shorter and idiomatic.
  • if len(a) == 0: is explicit, but usually unnecessary.

a == [] vs a is []

  • a == [] checks whether the list value is empty.
  • a is [] checks whether both sides are the exact same object.

That is why is should not be used here.

Empty list vs None

ValueMeaning
[]A real list exists, but it contains no items
NoneNo list value was provided
Take Quiz

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 a to check for empty.
  • Use a to check for non-empty.
  • Do not use is [].
  • Use is None only when checking for None.

Important edge case

if not a:

This is True for both:

  • a = []
  • a = None

If you need to tell them apart:

if a is None:
    ...
elif not a:
    ...

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.
  • None means 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.

Does this work for tuples, strings, and dictionaries too?

Yes. Empty collections are generally falsey in Python.

Take Quiz

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
Take Quiz

Keep learning

Related questions

View more