Python: Check a list is empty or not
Python List: Exercise-8 with Solution
Check if List is Empty
Write a Python program to check if a list is empty or not.
Visual Presentation:
Sample Solution:
Python Code:
# Create an empty list 'l'
l = []
# Check if the list 'l' is empty using the 'not' keyword
if not l:
# If the list is empty, print the message "List is empty"
print("List is empty")
Sample Output:
List is empty
Explanation:
In the above exercise -
l = [] -> This line creates an empty list called 'l'.
if not l: print("List is empty")
The above code checks if the list 'l' is empty using the not keyword, which checks if a value is False. If 'l' is empty, the condition is true, and the code block inside the if statement will execute.
Therefore, the output of this code will be "List is empty", since the list l is indeed empty and the code inside the if block is executed.
Flowchart:
Python Code Editor:
Previous: Write a Python program to remove duplicates from a list.
Next: Write a Python program to clone or copy a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/list/python-data-type-list-exercise-8.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics