2
Reply

Write Function in Python to Check whether list is empty or not ?

Ganesh Kavhar

Ganesh Kavhar

1y
648
0
Reply

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=[]

    Here below is the function to check the length of the list. ```python def is_list_empty(list):return len(list) == 0 ``` Now, you can use the above function to check the length of the list and if the list is empty it will return True otherwise False. ```python List1 = [""] if is_list_empty(List1):print("The list is empty") else:print("The list is not empty") ```

    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:

    1. def is_list_empty(lst):
    2. if len(lst) == 0:
    3. return True
    4. else:
    5. 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:

    1. my_list = []
    2. print(is_list_empty(my_list)) # Output: True
    3. my_list = [1, 2, 3]
    4. 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.