Member-only story

How to Check if a List is Empty in Python

Allwin Raju
3 min readFeb 3, 2025

--

Photo by Hitesh Choudhary on Unsplash

The other day, while working, I wanted to test if a list was empty in Python. Then, I remembered there are several ways to do it. Since I was feeling a bit bored, I decided to explore all the different methods to achieve this and figure out which one was the best. In this blog post, I’ll walk you through all the possible ways to check if a list is empty and share my thoughts on the most effective approach.

Read for free: https://allwin-raju.medium.com/how-to-check-if-a-list-is-empty-in-python-6857012ac703?sk=340f6935a752a78339920115b0dbe898

1. Using the not Keyword

Code Example:

my_list = []

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

Pros:

  • Most Pythonic: This is the recommended and most concise way to check for an empty list.
  • Readable: The code is clean and easy to understand.
  • Efficient: It directly evaluates the list’s boolean value without additional function calls.

Cons:

  • Implicit: For beginners, the implicit boolean conversion might be less intuitive compared to explicit checks like len().

2. Using the len() Function

--

--

No responses yet