Posted on 28th April 2025|51 views
How to check whether a list is empty or not in python?
Posted on 28th April 2025| views
I will explain it in two ways.
1. This is an example of checking a list is empty or not in a less pythonic way.
def Enquiry(li1):
if len(li1) == 0:
return 0
else:
return 1
li1 = []
if Enquiry(li1):
print ("not an empty list")
else:
print("List is empty")
2. Now let us view a more pythonic method to check for an empty list. That method of a check means an implicit method of checking also more excellent than the earlier one.
def Enquiry(li1):
if not li1:
return 1
else:
return 0
li1 = []
if Enquiry(li1):
print ("list is Empty")
else:
print ("list is not empty")