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.
Table of Contents
- Why do you check if a list is empty in python?
- Solution 1: Using PEP 8 recommended method
- Solution 2: Using the bool() function
- Solution 3: Using len()
- Closing thoughts
- Other Related Concepts
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.
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.
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() functionSimilar 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.
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.
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.
And since the condition is true, it returns the first value.
Closing thoughtsAs 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.