DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Edited on • Originally published at flexiple.com

How to check if a list is empty in python?

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, 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?

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.

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.

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

l2 = []

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

// Output: "list is empty"
Enter fullscreen mode Exit fullscreen mode

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.

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

l2 = []

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

// Output: "list is empty"
Enter fullscreen mode Exit fullscreen mode

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.

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

l2 = []

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

// Output: "list is empty"
Enter fullscreen mode Exit fullscreen mode

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.

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

l2 = []

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

// Output: "list is empty"
Enter fullscreen mode Exit fullscreen mode

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..

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"
Enter fullscreen mode Exit fullscreen mode

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. :)

Top comments (4)

Collapse
 
nisegami profile image
Arshad Hosein

One thing to note is that method 1 and 2 will also exhibit the same behavior if the list is None. However, this will raise an Exception with method 3. This is an important distinction and how you want to handle or not handle None should inform which you use (or what other checks you need). As for method 2 itself, it's just a more verbose method 1 and I wouldn't ever really recommend it be used personally.

Collapse
 
kamori profile image
Tyler Coil

Nice notes! Took me a bit to realize you were doing Lowercase L 1. I kept thinking you were writing eleven and twelve.

Collapse
 
hrishikesh1990 profile image
hrishikesh1990

Thank you for letting me know, Tyler.

I will ensure I follow a better naming scheme. :)

Collapse
 
mhsangar profile image
Mario Sánchez García

For me, I think Solution 3(b) is best just because the code is more readable. It's not always about making simpler code, your colleages need to understand it as quickly and easy as you!