python – check if list is empty

In this post, we will see how to check if list is empty in python.
It is very easy to check if list is empty in python. You can use "not" to check if list is empty in Python.
Let’s understand with the help of simple example.

list1=[1,2,3]
list2=[]
if not list1:
    print("list1 is empty")
else:
     print("list1 is not empty")

if not list2:
    print("list2 is empty")
else:
     print("list2 is not empty")

Output:

list1 is not empty
list2 is empty

You can also use Python len function to check if list is empty or not.

list3=[]
list4=['one','two','three']
if (len(list3)==0):
    print("list3 is empty")
else:
     print("list3 is not empty")

if (len(list4)==0):
    print("list4 is empty")
else:
     print("list4 is not empty")

Output:

list3 is empty
list4 is not empty

That’s all about python check if list is empty.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.