Write A Python Function To Check A List Is Empty Or Not

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Function To Check A List Is Empty Or Not

 

Title : 

Write A Python Function To Check A List Is Empty Or Not


Write A Python Function To Check A List Is Empty Or Not



 

Description : 

Hi guys, in this article you will learn a python program That uses a Python function to check if the given list is empty or not. The Python function returns true if list is empty, it returns false if list contains items. Define a function check_list_empty() and pass a list as input to the Python function and use the if statement to check if the list is not empty if so return true else return False.


    
def check_list_empty(items):
   if not items:
       return True
   return False


    


In the main program take two list variables list1 containing numbers and list2 is an empty list Now call the function check_list_empty() using the if statement to check if the list one is empty or not if so print the message appropriately using the print function call


    
list1 = [1, 2, 3]
list2 = []

print(f"Checking if {list1} is empty..")
if check_list_empty(list1):
   print("List is Empty")
else:
   print("List is not empty")



    


Now again call the function check_list_empty() by passing the list2 as an argument to it, use the if statement to check if the list is empty or not by comparing the return value. if the return value is true then print list is empty els print list is not empty.


    
print(f"Checking if {list2} is empty..")
if check_list_empty(list2):
   print("List is Empty")
else:
   print("List is not empty")



    




Complete Python Code : 


    
def check_list_empty(items):
   if not items:
       return True
   return False


list1 = [1, 2, 3]
list2 = []

print(f"Checking if {list1} is empty..")
if check_list_empty(list1):
   print("List is Empty")
else:
   print("List is not empty")

print(f"Checking if {list2} is empty..")
if check_list_empty(list2):
   print("List is Empty")
else:
   print("List is not empty")


    

 

 

Output : 


    
Checking if [1, 2, 3] is empty..
List is not empty

Checking if [] is empty..
List is Empty

    



Conclusion :

The above Python program uses the Python function that takes a list as input argument to it if the list is empty it will return true else it will return false. In the main program, call the above function by passing the first list containing items and the second list containing an empty list and note down the result. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments