python

Checking if List is empty in Python with Examples

Have you ever worked with an empty list and didn't know how to test if it was empty? It's actually easy to check if a list is empty in Python. Below are the different code examples that are used to check whether a List is empty or not in python.

fruit_list = []

# Using not keyword
if not fruit_list:
    print("List is empty")

# Using len() method
if len(fruit_list) == 0:
    print("List is empty")

You can also use '==' operator to check if a python list contains items or not. Below is the example to check an empty list with an equal operator.

fruit_list = []

if fruit_list == []:
    print("List is empty")
lst = []

if lst:
    print("List is not empty")
else:
    print("List is empty")
This is the most popular method to check whether a list is empty or not. All empty lists are considered false in the if condition. So can validate the list using this method.
The above list variable lst is empty so the program will print List is empty
def check_empty_list(lst):
  if bool(lst):
    print("List is not empty")
  else:
    print("List is empty")

check_empty_list([]) # -> List is empty

check_empty_list([1,2]) # -> List is not empty
We can also check empty list using python bool() method. it takes the list as a parameter and returns true if the list contains one or more items and returns false if it is empty.
In the above code example we have created a function check_empty_list() which takes the lists as a parameter and print the statement if list is empty or not.
my_list = [1]

if len(my_list):
    print("Not empty")
else:
    print("Empty")
The python len() method can also be used for checking an empty list. It also takes a list as a parameter and returns the count of items that a list contains. If the list is empty then it will return 0 and it is considered false.
Was this helpful?