In this article we will discuss four different ways to check if a list is empty or not. Then we will also see how to check if a list of lists is empty or not using for loop, List comprehension and all() function.

Check if a list is empty using “not” operator in python

In python, a sequence object can be implicitly convertible to bool. If sequence is empty then it evaluates to False else it evaluates to True. So, we can apply an if statement to a sequence object to check if it is empty or not.

Applying if statement to the list object

When ‘if statement’ is applied to a list, then it evaluates to False if list is empty, else it evaluates to True. If we apply ‘not’ operator along with ‘if statement’ to the list object, then it evaluates to True if list is empty else returns False.

Let’s see an example where we apply an ‘if condition’ and ‘not operator’ to check if a list is empty or not,

Output:

Check if list is empty using len() function

Python provides a built-in function len(),

It accepts a sequence like list, tuple or set etc and returns the number of elements in that sequence i.e. size of the sequence.
So, we can check the size of a list by passing the list object to len() function.Once we have the size of list then we can confirm if a list is empty by checking if its size is 0.

For example,

Output:

Check if list is empty using __len__()

List class has a special overloaded method,

It returns the number of elements in the list.
We can check the size of a list by calling __len__() function on the list object. Once we have the size of list then we can confirm if a list is empty by checking if its size is 0.

For example,

Output:

Check if list is empty by comparing with []

In python empty square brackets [] points to the empty list. So, we can check if our list object is empty or not by just comparing it with [] i.e.

Output:

Check if a list of lists is empty

There might be scenarios when we have a list of lists and we want to find out if all sub lists are empty. There are different ways to do that. Let’s discuss them one by one.

Check if a list of lists is empty using for loop

Suppose we have a list of lists,

We have created a function that accepts a list of lists and checks if all sub lists in the given list are empty,

This function accepts a list of lists, then iterates over all the sub lists in the main list using for loop and for each sub list it checks if it is empty or not using ‘if condition’ & ‘not operator’. If any of the sub list is non-empty then it returns False, where as if all sub lists are empty then it returns True

Let’s see use this function to check if list of lists is empty or not

Output:

Check if a list of lists is empty using List comprehension

Unlike the previous solution, here we will check if all sub lists in a given list are empty or not in a single like using List Comprehension and all() function.

Output:

List comprehension returned a list of bool , where each entry in this boolean list represents the sub list from the main list. If a sub list was empty then the corresponding entry in this bool list will be True else False. Then we passed this bool list to the all() function to check if all elements in this bool list are True or not. If all elements in the bool list are True, then it means all sub lists in the main list are empty.

The complete example is as follows,

Output:

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.