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

Leave a Reply

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