Python: Check if a List is empty

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

Table of Contents

Check if a list is empty using ‘not’ operator in python

In python, a sequence object can be implicitly convertible to bool. If the 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.

By applying if statement to the list object, we can check if it is empty pr not. For example,

# 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

How did it work?

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

Check if list is empty using len() function

Python provides a built-in function len(),

len(sequence)

It accepts a sequence like a list, tuple or set, etc, and returns the number of elements in that sequence i.e., size of the sequence.
We can check the list’s size by passing the list object to the len() function. Once we have the list size, 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

Python: Check if list is empty by comparing with empty list

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

This is not the quickest approach, because first empty list object will be created and then comparison will be done.

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 list size, 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 a list is empty using numpy

Convert a Python list to a numpy array and then check the numpy array size using attribute size. If the size of the numpy array is zeo then it means the list is empty. For example,

import numpy as np

# Create an empty list
list_of_num = []

arr = np.array(list_of_num)

if arr.size == 0:
    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

We have created a function that accepts a list of lists and checks if all sub-lists in the given list are empty or not. Let’s use this function to check if all lists in a list are empty or not.

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

# 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')

Output:

List of Lists is empty

This function check_if_empty() accepts a list of lists, then iterates over all the sublists 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-lists is non-empty, it returns False, whereas if all sub-lists are empty, it returns True.

Check if a list of lists is empty using List comprehension

Unlike the previous solution, here we will check if all sublists 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 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')

Output:

List of Lists is empty

List comprehension returned a list of bools, where each entry in this boolean list represents the sublist 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 the bool list elements are True, then it means all sub-lists in the main list are empty.

Summary

We discussed different ways to check if a list is empty or not. Then we also looked into the techniques to check if a list contains all empty lists 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.

Scroll to Top