There are multiple ways to check if a list is empty in Python, and the most efficient way is by using the Pep-8 style, called “Truth Value Testing“.
How to Check if a list is empty in Python
Lists are one of the four most commonly used data structures in Python, and it is mainly used to store data collection. There are multiple ways to find if the given list is empty or not in Python. Let us look into each of these approaches in detail with examples.
Solution 1: Using PEP 8 recommended Style.
In this approach, we will check if the list is empty in Python using its boolean value. It is also called as Truth Value Testing.
According to Python Official docs, the following built-in objects are considered false:
- constants defined to be false:
None
andFalse
. - zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
Since an empty list is just an empty collection, it will be converted to a boolean value of False
.
There are two ways we can check this. Let us take an example to demonstrate both methods.
In the first approach if the list contains the value then the if
condition evaluates to True and the code block inside the if
gets executed otherwise, the condition evaluates to False
and goes to the else
block.
my_list = []
if my_list:
print("List is not Empty")
else:
print("List is Empty")
Output
List is Empty
The second approach is similar but we just do an inversion by using an not
in the if
condition. In this case, if the list is empty the condition evaluates to True
. This is a better way to write code and also increases the readability of the code.
my_list = []
if not my_list:
print("List is Empty")
else:
print("List is not Empty")
Output
List is Empty
Solution 2: Using the bool() method
The other approach is to check if the list is empty or not by using the bool()
method. The bool()
function returns the boolean value of an object, i.e., either True
or False
.
The approach is less common as we can achieve the desired results without using the bool()
method, as shown in the PEP-8 recommended style.
my_list = []
if bool(my_list):
print("List is not Empty")
else:
print("List is Empty")
Output
List is Empty
Solution 3: Using the len() method.
The len()
method is another popular way to check if the list is empty in Python. The len()
method returns the length of an argument passed to it. If the length is 0, it means the list is empty.
There are two techniques that can be used here.
The first technique is by using Truth Value Testing. If the list is empty, then the len()
method returns 0
, which internally gets cast to the boolean value False
.
my_list = []
if len(my_list):
print("List is not Empty")
else:
print("List is Empty")
Output
List is Empty
The second technique is pretty simple. We will use the comparison operator to check if the length of the list is equal to 0. The main benefit of this technique is the code is more readable and easily understandable for Python beginners.
If the length of the list is equal to 0, then the condition satisfies, and we can conclude that it’s an empty list.
my_list = []
if len(my_list) == 0:
print("List is Empty")
else:
print("List is not Empty")
Output
List is Empty
Solution 4: Comparing with an empty list []
This approach is fairly simple for beginners. Here we are comparing an existing list with a new empty list []
inside an if condition. If the condition satisfies it means the list is empty and the if block gets executed otherwise it will execute the else block.
my_list = []
if my_list == []:
print("List is Empty")
else:
print("List is not Empty")
Output
List is Empty
Conclusion
There are several ways in Python to check if the list is empty, the most Pythonic way is to use the PEP-8 Style approach called Truth Value Testing. The other alternate approach is to use len()
method with the comparison operator or bool()
method.