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,

# Create an empty list
list_of_num = []

# Empty list object will evaluate to False
if not list_of_num:
    print('List is empty')
else:
    print('List is not empty')

Output:
List is empty

Check if list is empty using len() function

Python provides a built-in function len(),

len(sequence)

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,

# Create an empty list
list_of_num = []

# Check if list's size is 0
if len(list_of_num) == 0:
    print('List is empty')
else:
    print('List is not empty')

Output:
List is empty

Check if list is empty using __len__()

List class has a special overloaded method,

list.__len__()

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,

# Create an empty list
list_of_num = []

# Check if list's size is 0
if list_of_num.__len__() == 0:
    print('List is empty')
else:
    print('List is not empty')

Output:
List is empty

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.

# Create an empty list
list_of_num = []

# Check if list object points to literal []
if list_of_num == []:
    print('List is empty')
else:
    print('List is not empty')

Output:
List is empty

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,

# List of list
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,
def check_if_empty(list_of_lists):
    for elem in list_of_lists:
        if elem:
            return False
    return True

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

if check_if_empty(list_of_lists):
    print('List of Lists is empty')
else:
    print('List of Lists is not empty')

Output:
List of Lists is empty

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.

def check_if_empty_2(list_of_lists):
    return all([not elem for elem in list_of_lists ])

list_of_lists = [ [], [], [], []]

if check_if_empty_2(list_of_lists):
    print('List of Lists is empty')
else:
    print('List of Lists is not empty')

Output:
List of Lists is empty

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,

def check_if_empty(list_of_lists):
    for elem in list_of_lists:
        if elem:
            return False
    return True


def check_if_empty_2(list_of_lists):
    return all([not elem for elem in list_of_lists ])


def main():
    print('*** Check if a list is empty using "not in" operator ***')

    # Create an empty list
    list_of_num = []

    # Empty list object will evaluate to False
    if not list_of_num:
        print('List is empty')
    else:
        print('List is not empty')

    print('*** Check if list is empty using len() function ***')

    # Create an empty list
    list_of_num = []

    # Check if list's size is 0
    if len(list_of_num) == 0:
        print('List is empty')
    else:
        print('List is not empty')

    print('*** Check if list is empty using __len__() ***')

    # Create an empty list
    list_of_num = []

    # Check if list's size is 0
    if list_of_num.__len__() == 0:
        print('List is empty')
    else:
        print('List is not empty')

    print('*** Check if list is empty by comparing with [] ***')

    # Create an empty list
    list_of_num = []

    # Check if list object points to literal []
    if list_of_num == []:
        print('List is empty')
    else:
        print('List is not empty')

    print('**** Check if a list of lists is empty in python ****')

    print('*** Check if a list of lists is empty using for loop ***')

    # List of list
    list_of_lists = [ [], [], [], []]

    if check_if_empty(list_of_lists):
        print('List of Lists is empty')
    else:
        print('List of Lists is not empty')

    print('*** Check if a list of lists is empty using List comprehension ***')

    # List of list
    list_of_lists = [ [], [], [], []]

    if check_if_empty_2(list_of_lists):
        print('List of Lists is empty')
    else:
        print('List of Lists is not empty')


if __name__ == '__main__':
   main()

Output:
*** Check if a list is empty using "not in" operator ***
List is empty
*** Check if list is empty using len() function ***
List is empty
*** Check if list is empty using __len__() ***
List is empty
*** Check if list is empty by comparing with [] ***
List is empty
**** Check if a list of lists is empty in python ****
*** Check if a list of lists is empty using for loop ***
List of Lists is empty
*** Check if a list of lists is empty using List comprehension ***
List of Lists is empty

Subscribe with us to join a list of 2000+ Programmers for weekly newsletter.