How To Check If a List Is Empty in Python

Learn multiple techniques to check for an empty list

Jonathan Hsu
Nov 17, 2019 · 3 min read
Image for post
Image for post
Photo by Andrew Neel on Unsplash

There are many options to check if a list is empty. Before getting into the syntax of the solutions, let’s lay out the different factors involved in deciding which method we want to use.

The expression we craft can fall into one of two camps, an explicit comparison to an empty list or an implicit evaluation of an empty list. What does that mean?

Explicit Comparison

Let’s begin with the explicit comparison. Whether we use list notation [] or an empty function list(), the strategy is to see if our list in question is exactly the same as an empty list.

# both create an empty list
a = []
b = list()
print(a == b) # True

Additionally, we can use the len() function to return the actual count of items in the list.

a = []if len(a) == 0:
print("The list is empty")

Implicit Evaluation

Conversely, an implicit evaluation leverages the way in which an empty list will evaluate as a False boolean value and a populated list will evaluate as a True boolean value.

a = []
b = [1]
if a:
print("Evaluated True")
else:
print("Evaluated False")
if b:
print("Evaluated True")
else:
print("Evaluated False")
"""
Evaluated False
Evaluated True
"""

So, what difference does it make?

I have developed the habit of using explicit comparisons; however, if you are trying to follow the practice of duck typing, then you want to go with the implicit method.

What Is Duck Typing?

The term duck typing comes from the phrase:

If it walks like a duck and it quacks like a duck, then it must be a duck.

Functionally, it’s an acknowledgment that less stress will be put on the object’s actual data type. Rather, emphasis will be placed on its attribute’s behaviors, such as being iterable. Ultimately, the goal is to become type agnostic.

Duck typing prioritizes convenience over safety, allowing for more flexible code that can adapt to a wider range of uses and is less strict than traditional conventions.

Which Technique Should I Use?

I’ve become increasingly comfortable with, and as a result preferential towards, an implicit evaluation, understanding that an empty list will evaluate to False.

a = []print(bool(a)) # False

This allows me to consolidate longer expression checks, such as:

# BEFORE
if isinstance(a,list) and len(a) > 0:
print("Processing list...")
# AFTER
if a:
print("Processing list...")

Ultimately, the choice will come down to implications of an empty list.

Are you checking if a list is empty because you want to iterate over it? Implicit evaluation is the path of least resistance.

Are you checking for an empty list because your code is planning on using list methods next? I would probably choose an explicit comparison to also validate data type in that scenario.

Which method do you prefer? What are the determining factors when deciding on the technique you will use to check for an empty list?

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store