How to check if a list is empty in python?
Tags: python, programming, tutorial
Published: 2021-05-15 11:21:25

In this short tutorial, find how to check if a list is empty in python. We also look at why you need to do this so that you understand the purpose better.

This tutorial is a part of our initiative at  [Flexiple](https://flexiple.com/), to write short curated tutorials around often used or interesting concepts.

### Table of Content

- [Why do you check if a list is empty in Python?](#why-do-you-check-if-a-list-is-empty-in-python)
- [Solution 1: Using PEP 8 recommended method](#solution-1-using-pep-8-recommended-method)
- [Solution 2: Using the bool() function](#solution-2-using-the-bool-function)
- [Solution 3: Using len()](#solution-3-using-len)
- [Closing thoughts](#closing-thoughts)

## Why do you check if a list is empty in python?
While dealing with lists, a major characteristic most developers make use of is its iterability. This means that you can iterate through the values in the list making it suitable for loops especially `for`. This also comes in handy while working with strings and numerical operations. And hence it is a good practice to check if a list is empty before proceeding.

This remains true for all iterables i.e dictionaries, tuples, etc. 

With that out of the way, let us look at the various methods that can be used to check if a list is empty in Python.

## Solution 1: Using PEP 8 recommended method:
Solution 1 & 2 make use of a method called **Truth Value Testing**. What this essentially means is that we check if the list is empty using its boolean value. This is possible because, in Python empty sequences, positional arguments containing 0, 0.0 or with length 0, are all considered to be `false`. You can read more about this [here](https://docs.python.org/3/library/stdtypes.html#truth-value-testing).

Because of this method, we can check if a list is empty in Python. And below is the most Pythonic way of checking the same.


```python
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if l2:
    print("list is not empty")
else:
    print("list is empty")

// Output: "list is empty"
``` 

Since an empty list is `False`, the condition is false and hence we are able to identify an empty list. Feel free to change the condition with l1.

Another common method is with the Implication of a `not`.

```python
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if not l2:
    print("list is  empty")
else:
    print("list is not empty")

// Output: "list is empty"
``` 

This is a similar approach however we use a not in the loop. It inverses the value and hence the condition becomes true. This method is used to increase readability as a developer could type the desired code under the `else`.

## Solution 2: Using the bool() function
Similar to the first method, we check if a list is empty using the `bool()` function. The `bool()` function returns the boolean value of an object i.e true or false. The code is also very similar to the first method. Choosing between the two methods would boil down to a personal choice.


```python
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if bool(l2):
    print("list is empty")
else:
    print("list is not empty")

// Output: "list is empty"
``` 
And since the value is `false` the print under the `else` is returned. 

## Solution 3: Using len()
In this solution, we use the `len()` to check if a list is empty, this function returns the length of the argument passed. And given the length of an empty list is 0 it can be used to check if a list is empty in Python.

Here again, there are two techniques that can be used. The first method is based on the Truth Value Testing, 0 is considered `false`. 

```python
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if len(l2):
    print("list is not empty")
else:
    print("list is empty")

// Output: "list is empty"
``` 
Here since the `len()` of l2 is 0 it is considered `false` and hence the condition returns the output under the else. 

In the other methods, we use a condition to compare the length of the list with 0. Although this method is very similar to the first method this is mainly used to help improve readability..  

```python
l1 = ["Hire", "the", "top", "1%", "freelancers"]

l2 = []

if len(l2) == 0:
    print("list is empty")
else:
    print("list is not empty")

// Output: "list is empty"
``` 
And since the condition is true, it returns the first value. 

## Closing thoughts
As you have seen there are multiple ways through which you can check if a list is empty in Python. And it is a good practice to use this condition before and then nest your `if` or for loops, this would help reduce unwanted errors.

And as to which solution would be the best choice, it again boils down to your knowledge of the language. If you are a beginner I would suggest you use the methods with the `len( )== 0` as it is straightforward and readable. If you are proficient you can use solutions 1 or 2 but again I would recommend using the not l2 solution as it is more readable. 

Do let me know your thoughts in the comments section below. :)