Write Function in Python to Check whether list is empty or not ?
Ganesh Kavhar
Select an image from your device to upload
Suppose we have a empty List as below so wht will be a function code in python to check whether it is empty or not .
List1=[]
To check if a list is empty in Python, you can use the len() function. The len() function returns the number of elements in a list. If the length of the list is 0, it means the list is empty.
Here is a simple function that checks if a list is empty:
def is_list_empty(lst): if len(lst) == 0: return True else: return False
def is_list_empty(lst):
if len(lst) == 0:
return True
else:
return False
You can use this function by passing your list as an argument. It will return True if the list is empty and False otherwise.
For example:
my_list = []print(is_list_empty(my_list)) # Output: Truemy_list = [1, 2, 3]print(is_list_empty(my_list)) # Output: False
my_list = []
print(is_list_empty(my_list)) # Output: True
my_list = [1, 2, 3]
print(is_list_empty(my_list)) # Output: False
In the above code, is_list_empty() takes a list as an argument and checks if its length is equal to 0. If it is, the function returns True, indicating that the list is empty. Otherwise, it returns False.
This function provides a simple and efficient way to determine whether a list is empty or not in Python.