How To Check If a List Is Empty in Python

How To Check If a List Is Empty in Python

Learn multiple techniques to check for an empty list

Image for postPhoto 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 lista = 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 FalseEvaluated 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:

# BEFOREif isinstance(a,list) and len(a) > 0: print(“Processing list…”)# AFTERif 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?


No Responses

Write a response