Python Check If List is Empty[4 Methods]

In this Python tutorial, you will learn about Python check If list is empty using different methods and multiple examples.

While working on an e-commerce website project in Python, I needed to check whether the cart was empty so I could send an alert message to the user that the cart was empty.

In this situation, I searched for a solution and found four different ways to check whether the list was empty.

4 Ways to Check If List is Empty

Let’s understand the scenario more clearly with one practical example; suppose this is my list of products and list of carts like this,

products = ["soap", "shampoo", "brush","toothpaste"]

cart = [] 

If the user tries to checkout without adding any products to the cart, an alert message should be printed that the cart is empty like this,

Your Cart Is Empty

To check whether the cart is empty, we will explore each method individually with practical examples.

Python Check If List is Empty Using len() Method

First, we will use the len() method to check whether the list is empty or not in Python. Generally, the len() method in Python returns the count of the elements. So we can compare the length of the list with 0.

READ:  Build Artificial Neural Network in Tensorflow

Syntax

if len(list) == 0:
  • If len(list) == 0, this syntax will check the equality of 0 and the length of the list.

Let’s execute a practical example of How Python check if list is empty or not.

products = ["soap", "shampoo", "brush","toothpaste"]

cart = [] 

if len(cart) == 0:
    print("Your Cart is empty")
else:
    print("Thanks for Purchasing...")
Python Check If List is Empty Using len() Method

In the above code, we have a list of products and a list of cart. I needed to check whether the cart was empty or not. So we used the len() method like this: if len(cart) == 0, it will compare the count of elements in the cart list to the 0.

Check if the list is empty in Python Using the bool() Method

We can also use the bool method to check that the variable contains some values, or is empty. The bool() method in Python is used to check the existence of the value and return True or False based on it.

Syntax

  • if bool(list): If the list contains some values, it will return True; otherwise, it will return False.

Let’s understand how Python checks if list is empty or not using the bool() method in Python.

check_list = []

if bool(check_list):
    print("List is not empty")
else:
    print("List is empty")
Check if the list is empty in Python using Using bool() Method

In the above code, we have a list named check_list. So, we need to check whether it contains some values or not. Here, we used the bool() method like this:
if bool(check_list), so here our list is empty; it returns false by executing the else statement block.

Check Empty List Python using the == operator

We can also use the == operator, which is a comparison operator in Python. It checks the equality between two values and returns a boolean value(True or False) based on that equality.

READ:  Scipy Stats Zscore + Examples

So, we will compare the list with empty square brackets.

Syntax

if cart == []
  • cart == []: Here, we are directly comparing two values and getting True or False.

Let’s understand how Python check if list is empty using the == operator.

products = ["soap", "shampoo", "brush","toothpaste"]

cart = []

if cart == []:
    print("Cart is empty")
else:
    print("Cart is not empty")
Python Check If List is Empty using the == operator

In the above code, again, we took a list of products and a cart list. This time, we check whether the list is empty or not using the == operator in Python. if cart == [], Here we are comparing two list values.

How to Check Python List is Empty using not Operator

Here, we will use the “not” operator, a logical operator in Python, to check whether the list is empty. The not operator reverses the result of the condition. Suppose the condition returns True, and you are using the not operator with it; it will turn it to False.

Syntax

if not <condition>

Let’s understand how Python check if list is empty using not operator

products = ["soap", "shampoo", "brush","toothpaste"]

cart = []

if not cart:
    print("Cart is Empty!")
else:
    print("False")
How to Check Python List is Empty using not Operator

In the above code, we have a list of products and a cart. We used not operator like this:
if not cart. Here, the cart was empty, and if it is like this if cart: it means there were items in the cart, and it would print ‘False’.

But we used no operator with it, so it will make it True and execute the if statement block.

Conclusion

In this Python article, you learned about Python check if the list is empty using different methods, such as the len() method, the not operator, the == operator, and the bool() method.

READ:  PyTorch Load Model + Examples

You may like to read: