Python list empty check using Boolean logic

Python Boolean Data Type: Exercise-4 with Solution

Write a Python function that checks if a given list is empty or not using boolean logic.

Sample Solution:


def is_empty_list(lst):
    return not bool(lst)

def main():
        input_list = eval(input("Input a list (e.g., [1, 2, 3, 4]): "))
        if isinstance(input_list, list):
            if is_empty_list(input_list):
                print("The list is empty.")
                print("The list is not empty.")
            print("Invalid input. Please enter a valid list.")
    except Exception as e:
        print("An error occurred:", e)

if __name__ == "__main__":


Input a list (e.g., [1, 2, 3, 4]): [1,2,3]
The list is not empty.
Input a list (e.g., [1, 2, 3, 4]): []
The list is empty.

In the above exercise the program prompts the user to input a list (using eval to convert the input string to a list), and then uses the "is_empty_list()" function to determine if the list is empty or not. Based on the results, it prints the appropriate message.


Flowchart: Python list empty check using Boolean logic.

Previous: Voting eligibility checker.
Next: Python leap year checker using Boolean logic.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.