Python Program to Check If a List is Empty – 2024

Hey guys, in this blog we will see a Python Program to Check If a List is Empty.

Table of Contents

Example 1: Using the Boolean operation

# Python Program to Check If a List is Empty 

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

Output

The list is empty

  • This is the easiest method to check an empty list.
  • If my_list is empty it will work as a False.
  • We have used if not my_list which is equivalent to if not False or if True, which shows that the if statement is True and it will print ‘the list is empty’.

Example 2: Using len()

# Python Program to Check If a List is Empty 

my_list = []
if not len(my_list):
    print("The list is empty")

Output

The list is empty

  • Here also we are doing something similar to the first method.
  • Here we will find the length of the list.
  • If the length of the list is 0, then the, if statement will become True/1 and will print ‘the list is empty’.

Example 3: Comparing with []

# Python Program to Check If a List is Empty 

my_list = []
if my_list == []:
    print("The list is empty")

Output

The list is empty

Here we will simply compare our list with [] and if it is True, it means our list is empty and print ‘The list is empty’.

Check out our other python programming examples

Abhishek Sharma
Abhishek Sharma

Started my Data Science journey in my 2nd year of college and since then continuously into it because of the magical powers of ML and continuously doing projects in almost every domain of AI like ML, DL, CV, NLP.

Articles: 520

Subscribe to our Newsletter

Subscribe to our newsletter and receive all latest projects...

Leave a Reply

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