How to Check if a List is Empty in Python
Tags: programming, beginners, python, tutorial
Published: 2019-02-21 15:16:43

---
title: How to Check if a List is Empty in Python
published: true
description: Manipulating lists can be fun, but it can also lead to some frustrating bugs if we're not careful. After all, some lists are empty. Today, we'll learn how to check if a list is empty in Python.
tags: programming, beginners, python, tutorial
canonical_url: https://therenegadecoder.com/code/how-to-check-if-a-list-is-empty-in-python/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4bmkvjhpxcomh160w6vb.jpg 
series: How to Python
---

In an effort to start cross-posting, I figured I'd share another brief Python article with everyone. Today, we’re going to learn how to check if a list is empty in Python.

## Problem Introduction

So far, we’ve played a lot with data structures like lists and dictionaries. In terms of lists, we’ve learned [how to sum elements of two lists][1], and we’ve even learned [how to convert two lists into a dictionary][2]. However, in all this list manipulation, we’ve never actually discussed how to check if a list is empty.

Detecting an empty list is an important skill to learn as a Python developer. It allows us to handle errors that can occur when attempting to manipulate an empty list. For example, we all know what happens when we try to index an empty list:

```shell
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> my_list = list()
>>> my_list[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
```

If we happen to have a function that does some indexing, maybe it’s a good idea to add a protective check for an empty list. After all, handling edge cases is an important skill to learn for any coder.

## Solutions

When it comes to checking if a list is empty in Python, there are a handful of solutions. Let’s dive into a few.

### Check if a List is Empty by its Length

Alright, so the first method for checking if a list is empty is to verify that the length of the list is zero. As someone who learned Java first, I find this solution very intuitive. Take a look:

```python
my_list = list()
if len(my_list) == 0:
    pass  # the list is empty
```

The drawback of this method is that it isn’t very pythonic. After all, the Python community is very strict about its idioms, so it’s probably a good idea to avoid this method if possible.

### Check if a List is Empty by Direct Comparison

Another solution, as mentioned by [vorsprung][4], is to compare a list directly to an empty list:

```python
my_list = list()
if my_list == []:
    pass  # the list is empty
```

This solution works just as well as the previous version, but it's also not considered pythonic. The reason being that Python has two different types of list-like collections: tuples and lists. Tuples are immutable lists, and comparing a empty tuple to an empty list, `() == []`, would return false. 

Regardless, both solutions are valid. In the next section, we'll cover the community accepted list emptiness test.

### Check if a List is Empty by its Type Flexibility

Dynamic typing is a fun feature of Python because it allows a variable to assume many forms. While someone with a static typing background might find this frustrating, others will find that dynamic typing in Python has its perks. Among those perks is the fact that [empty sequences evaluate to false][3].

So, what does that mean in terms of code? Well, it means we can treat a list like a boolean. For example:

```python
my_list = list()
if not my_list:
    pass  # the list is empty
```

Now, this may not seem very intuitive, but it is the accepted PEP 8 standard. In other words, this is the method we should use in production code. 

## A Little Recap

Today’s topic is how to check if a list is empty in Python. As always, in this section, we do a little recap of the solutions we’ve shared above:

```python
my_list = list()

# Check if a list is empty by its length
if len(my_list) == 0:
    pass  # the list is empty

# Check if a list is empty by direct comparison
if my_list == []:
    pass  # the list is empty

# Check if a list is empty by its type flexibility **preferred method**
if not my_list:
    pass  # the list is empty
```

And with that, we’re finished. Once again, thanks for stopping by. Hopefully, this was helpful!

[1]: https://therenegadecoder.com/code/how-to-sum-elements-of-two-lists-in-python/
[2]: https://therenegadecoder.com/code/how-to-convert-two-lists-into-a-dictionary-in-python/
[3]: https://www.python.org/dev/peps/pep-0008/#programming-recommendations
[4]: https://dev.to/vorsprung/comment/9192