How To Check If A List Is Empty In Python
In this article, we will see how to check if a list is empty or not.
A list is said to be empty when there are no elements in the list.
Method 1:
def check(li): if len(li) == 0: return 1 else: return 0 list1 = [1, 2, 3, 4, 5] list2 = [] for l in (list1, list2): if check(l): print("List is empty") else: print("List is not empty")
Output:
List is not empty List is empty
Method 2:
def check(li): if not li: return 1 else: return 0 list1 = [1, 2, 3, 4, 5] list2 = [] for l in (list1, list2): if check(l): print("List is empty") else: print("List is not empty")
Output:
List is not empty List is empty
Subscribe
Login
Please login to comment
0 Discussion