Examples

Python Program to Check If a List is Empty

Python lists are methods for storing several objects together. For example, if I want to consolidate my shopping list items into one variable, I would write a list rather than creating one variable for each shopping item. As a programmer, we sometimes need to check if a list is empty or not before executing any operations on it in Python. Because attempting to perform operations on an empty list will result in an error. In this article, we’ll look at how to determine whether a given list is empty or not.

Check if the List is empty or not

So far, we have learned a lot of different functions related to Python lists. However, in all this list manipulation the most important task is to first check if the list is empty. Detecting an empty list, in any case, is a vital skill to acquire as a Python developer. It enables us to deal with mistakes that may arise while attempting to manipulate an empty list.

When it comes to checking if a list is empty in Python, there are a few options, including:

  • Ensure that the list’s length is zero.
  • Directly comparing the list to another empty list
  • Using the Boolean operator to check for the absence of a value
  • The list’s type flexibility is used to ensure that it is empty (preferred method)

Let us learn about each of these methods in detail.

Example 1: Using the Boolean operation

Any object, including a list, can have its value evaluated as a Boolean value, either true or false, and this value is returned to you. Unless they are empty, all list objects have a value of True. Empty sequences are all considered False in Python. The PEP8 style standard recommends this method since it is strongly Pythonic.

To verify if a list element is empty in Python, use the example below, which employs the Python Boolean not statement. It inverses the value, causing the condition to become True. This approach is used to improve readability by allowing a developer to type the desired code under else.

Example

                    

my_list = []

if not my_list:
    print('The list is empty')
else:
    print('The list is not empty')

Output

                    

The list is empty

By using this method, we can see that If my list is null/empty, the Boolean statement not returns True.

Example 2: Using len()

In this approach, we utilize the len() function to determine whether or not a list is empty; this function returns the length of the argument supplied. And, because the length of an empty list is 0, it may be used in Python to determine if a list is empty.

Example

                    

my_list = []

# Method 1
if not len(my_list):
    print('The list is empty')

# Method 2
if len(my_list) == 0:
    print('The list is empty')

Output

                    

The list is empty
The list is empty

Example 3: Comparing with []

Because it involves comparing with an empty list, this method is likewise quite easy and suitable for novices. This is useful if you want to make it obvious what you’re comparing against. This kind of comparison is also not advised in Python, despite the fact that it may be semantically accurate.

Example

                    

my_list = []

if my_list == []:
    print('The list is empty')
else:
    print('The list is not empty')

Output

                    

The list is empty

Example 4: Using bool() function

In addition to the methods listed above, you can utilize the Python bool() function. If the list is empty, the bool function returns False. However, if the list is not empty, the bool() function returns True.

Example

                    

my_list = []

if bool(my_list) == False:
    print('The list is empty')
else:
    print('The list is not empty')

Output

                    

The list is empty

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.