python – check if list is empty

Previous
Next

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.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog