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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
list2 is empty
You can also use Python len function to check if list is empty or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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
list4 is not empty
That’s all about python check if list is empty.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.