How to Check If List is Empty or not Empty in Python

In this tutorial, learn how to check if list is empty or not using Python. Check whether a list is empty with the description and examples are given here.

There are many methods from which 3 methods are given here to find the empty list. The short answer is: use the Python len() with the if condition to find if the list is empty. So, let’s start learning each method given below.

Check If List is Empty with If and len() in Python

If you want to find the list if it is empty, you have to use the Python len(). The length of the list is zero(0) in the output. It means the list is empty and there are no elements in the given list.

There is also an else statement with the if statement to print false output. If the list has some items, the output executes the code under the else statement.

Output

List is empty.

The above example showing the empty list and the code to find whether the list is empty. The output prints the text showing that the list contains no elements and it is empty.

Get Existence of Elements with If and Boolean Context in Python

You can check the list if it is empty using the boolean context. To perform this, you have to use the below-given example using the Python boolean not statement. If the list contains no element, it executes the print function under the if condition.

Output

List is empty.

The above example showing the above-given list of Python is empty. However, if the list is not empty, it executes the code under the else statement of Python.

Find Empty List Using If and bool() in Python

In addition to above all methods, you can also use the Python bool() function. The bool function gives the output as False, if the list is empty. But, if the list is not empty, the bool function given the output as True.

You have to check the list if empty using the below-given example. Change the example as per your requirement to get the result.

Output

List is empty.

The above example prints the code under if condition, if the list is empty and bool function gives False. Otherwise, it prints the code under the else statement if the bool function given True.

You may also like to read

Hope, you like this post on how to check if the list is empty using Python. If you have any query regarding the tutorial, please comment below.

References

Also tell me, which method you are using to find your list if it is empty or not.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.