Python – Check if list is empty or not

In this article, we will learn different ways to check if a list is empty or not in python.

Python list is an order sequence that can hold multiple object types like integers, strings, and other objects in a single variable.

A list is an iterable object, so in some cases, we might want to check if the list has any items in it or if it’s an empty list.

How to find out if a python list is empty

It is easy to find an empty list in Python because any empty sequence like ('',(),[]) will evaluate to False .

So, if we check an empty list with in-built Python methods like len(), bool() it will return False.

Ways to check for empty list in Python

To check if a Python list is empty, we can use any of the following methods:

  1. len() method
  2. not operator
  3. bool() method
  4. Comparing to an empty list

let’s see methods with an example.

Using len() to check for empty lists in Python

The len() function returns the number of elements in a list in Python. If len() returns zero it means the list is empty.

Example:

my_list = []

if len(my_list) == 0:
    print('List is Empty')
else:
    print('List is not Empty')    

Output:

List is Empty

Here, we have an empty list in a variable my_list.

We used the equality operator (==) to compare the result of len() function with 0. Since the len(my_list) returned zero, the comparison returned True too. Thus the if statement executed the first condition and printed out “List is Empty“.

Using not operator to check for empty Python List

The not operator is a logical operator in Python. It changes a boolean value to the opposite with the not keyword.

If the value is True, it will change it to False (not True) and if the value is False, it will change it to True (not False)

As we know, any empty sequence will evaluate to False, the not operator will switch it to True. For example.

my_list = []

if not my_list:
    print('List is Empty')
else:
    print('List is not Empty')   

Output:

List is Empty

Here, we have a variable (my_list) with an empty list. Next, we use the if statement and the not operator to check if the list is empty or not.

Now, if the list is empty the not my_list statement will turn True, and so it will yield out “List is Empty” in the terminal.

Using bool() method to check for empty list in Python

The bool() function is used to return a boolean value i.e True or False, of any specified object in python.

list1 = []
list2 = [1,2,3]

print(bool(list1)) # False
print(bool(list2)) # True

So, we can use the bool() method to find an empty Python list like this.

Example:

my_list = []

if bool(my_list):
    print('List is Not Empty')
else:
    print('List is empty')   

Output:

List is Empty

Here, we have passed our Python list in the bool function and since the list is empty it returns False. And so it executes the else statement i.e List is Empty.

Check if list is empty by comparing it with an empty list []

We can use the equality operator (==) to compare our Python list with an empty list ([]). It will return True, if both the list are equal, if not it will return False.

We can use the if statement to print out a desired output on the terminal.

Example:

my_list = []

if my_list == []:
    print('List is Empty')
else:
    print('List is not empty')   

Output:

List is Empty

Since the list is empty, it runs the first block of the if statement and prints “List is Empty” in the terminal.

Conclusion: In this post, we have learned how to check if a python list is empty using in-built python functions like bool() and len(), using not operator and also using equality operator.


Related topics:

Python – Check if a string is Empty or Not

Check if user input is empty in python

Get the Index or Position of Item in List in Python

How to split a list into multiple list using python

Remove Duplicates from List in Python

Prepend List in Python

Python – Insert an element at specific index in a list

Python – Check if list is empty or not

Remove the Last N element from a List in Python

Scroll to Top