Check if List of Lists is Empty in Python (2 Examples)

 

Hi there! In this tutorial, I will show you 2 simple methods of checking whether a list of lists or a nested list is empty in the Python programming language.

First, here is a quick overview of this tutorial:

Let’s get right into it!

 

What is a List of Lists?

A Python list can contain any object, including other lists, which are called “sublists”. These sublists themselves can also contain other lists, and so on. This is what is called a list of lists or a nested list in the Python programming language.
 

Create List of Lists

Here, we are going to create a nested list that we will use in this tutorial. Run the code below in your preferred Python IDE.

mylist = [[1,2,3],[4,5],6,[],9,[]]

The list above contains separate 4 lists: two occupied and two empty lists.
 

Example 1: Define Custom Function

In this example, we are going to define a custom function for checking if a list is empty in Python. Then, in a for loop, we are going to iterate through the content of our sample list and parse that iteration through the function we defined:

def is_empty(list_item):
  if list_item == []:
    return True
  else:
    return False

The function above simply checks if a list item is an empty list. If it is, it returns True, if not, then it returns False.

Let us now run the for loop:

result = []
for i in mylist:
  result.append(is_empty(i))
 
print(result)
 
# [False, False, False, True, False, True]

The returned array of boolean values tells us which sublists are empty according to their respective index positions. Sublists 3 and 5 are the empty ones because indexing in Python starts from 0.

We can also use Python’s all() function to summarize our result. The True value will be returned, if all values in the array are True which means that the list of lists is empty. Otherwise, the False value will be returned, which means that at least one sublist is non-empty within the list, hence the list of lists is not empty.

if all(result) == True:
  print(True)
else:
  print(False)
 
# False

 

Example 2: Use all() & map() Functions

In this example, we combine Python’s all() and map() functions to determine whether the list of lists is indeed empty or not. We will also parse the custom is_empty() function to the map() function, which will run an iteration through it.

all(map(is_empty, newlist))
 
# True

It returns True because all the sublists in the nested list are empty. We can try it with mylist and see what it returns.

all(map(is_empty, mylist))
 
# False

It returns False because all the sublists are not empty.

That is how to check whether a list of lists is empty in Python. Hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to check if a list of lists is empty in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to check if a list of lists is empty in Python.

 

The YouTube video will be added soon.

 

Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to check if a list of lists is empty in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top