w3resource

Python: Check a list is empty or not


Check if List is Empty

Write a Python program to check if a list is empty or not.

Visual Presentation:

Python: Check a list is empty or not

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:

Flowchart: Check a list is empty or not

For more Practice: Solve these Related Problems:

  • Write a Python program to check if a given nested list is completely empty.
  • Write a Python program to check if a list contains only None values.
  • Write a Python program to check if all elements in a list are zero.
  • Write a Python program to check if a list contains at least one non-empty sublist.

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.



Follow us on Facebook and Twitter for latest update.