python
coding
check
empty
list

How do I check if a list is empty?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In Python, you can check if a list is empty in several ways. Here are the most common and Pythonic methods:


The most Pythonic way to check if a list is empty is to take advantage of its "truthy" or "falsy" value.

Example:

python
1my_list = []
2
3if not my_list:
4    print("The list is empty!")
5else:
6    print("The list is not empty.")

Why it Works:

  • An empty list evaluates to False in a conditional statement.
  • A non-empty list evaluates to True.

2. Using len()

You can check the length of the list with the len() function.

Example:

python
1my_list = []
2
3if len(my_list) == 0:
4    print("The list is empty!")
5else:
6    print("The list is not empty.")

Explanation:

  • len(my_list) returns 0 if the list is empty.

3. Using == []

You can directly compare the list to an empty list [].

Example:

python
1my_list = []
2
3if my_list == []:
4    print("The list is empty!")
5else:
6    print("The list is not empty.")

Note:

While this works, it is less Pythonic than using if not list.


4. Using any() (For Non-Empty Values)

If you want to check if a list has any non-empty values, use any().

Example:

python
1my_list = [0, "", None]
2
3if any(my_list):
4    print("The list has truthy elements!")
5else:
6    print("The list is empty or contains only falsy values.")

Explanation:

  • any() checks if at least one element is truthy.

5. Checking Directly with bool()

You can explicitly convert the list to a boolean using bool().

Example:

python
1my_list = []
2
3if not bool(my_list):
4    print("The list is empty!")
5else:
6    print("The list is not empty.")

Explanation:

  • bool([]) evaluates to False for an empty list.
  • bool([1, 2, 3]) evaluates to True for a non-empty list.

Comparison of Methods

MethodDescriptionPythonic
if not listChecks if the list evaluates to False✅ Yes
len(list) == 0Checks length of the list⚠️ Less Pythonic
list == []Compares list directly to []⚠️ Less Pythonic
bool(list)Explicitly converts list to a boolean✅ Yes
any(list)Checks for any truthy elements✅ Depends on case

The most Pythonic and efficient method to check if a list is empty is:

python
if not my_list:
    print("The list is empty!")

This method is clean, concise, and widely adopted in Python code. 🚀


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.