python – check if list is empty

In this post, we will see how to check if list is empty in pyhton.
There are many ways to do it. We will see most simple ways to check if list is empty in Python.
You can simply use “not” to check if list is empty or not.

Let’s understand with the help of example.

l=[] if not l: print("List l is empty") else: print("List l is not empty") l1=[1,2,3] if not l1: print("List l1 is empty") else: print("List l1 is not empty")

Output:

List l is empty
List l1 is not empty

You can also use length to check if list is empty or not.

l=[] if len(l)==0: print("List l is empty") else: print("List l is not empty") l1=[1,2,3] if len(l1)==0: print("List l1 is empty") else: print("List l1 is not empty")

Output:

List l is empty
List l1 is not empty

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

Leave a Reply

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