Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 15, 2021 11:21 am GMT

How to check if a list is empty in python?

In this short tutorial, we look at the different methods that you can use 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"

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"

This is a similar approach however we use a not in the loop what this does is it inverses the value and hence the condition become 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"

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"

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"

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


Original Link: https://dev.to/hrishikesh1990/how-to-check-if-a-list-is-empty-in-python-17ob

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To