How to Check if Python List is Empty?

Python – Check if list is empty

To check if Python List is empty, you can write a condition if the length of the list is zero or not; or you can directly use the list reference along with not operator as a condition in if statement.

Following is the syntax to use not operator and the list as a condition to check if the list is empty.

if not myList:
    statement(s)

In the above If statement, statement(s) inside if block execute only if myList is not empty.

Following is the syntax to use the length of list to form a condition to check if the list is empty.

if len(myList) == 0:
    statement(s)

If the list is empty, then there would be zero or no elements in the list. Use builtin function len() and pass the list as argument. len() function returns an integer representing the number of elements in the list. If the list is empty, len() returns 0 and the condition len(myList)==0 becomes True.

Examples

1. Check if given list is empty using if-not

In the following program, we take an empty list, and then check programmatically if the list is empty or not using not operator and list reference in if-condition .

Python Program

myList = []
if not myList:
    print('The list is empty.')
else:
    print('The list is not empty.')
Run Code Copy

Output

The list is empty.

2. Check if given list is empty using len()

In the following program, we take an empty list, and then check programmatically if the list is empty or not using len() built-in function.

Python Program

myList = []
if (len(myList) == 0):
    print('The list is empty.')
else:
    print('The list is not empty.')
Run Code Copy

Output

The list is empty.

Summary

In this tutorial of Python Examples, we learned how to check if a Python List is empty or not.

Related Tutorials

Quiz on

Q1. Which of the following represents an empty List?

Not answered

Q2. Which of the following statement returns an empty List?

Not answered

Q3. What is the length of an empty List?

Not answered

Q4. Which of the following condition returns True for an empty List myList?

Not answered
Code copied to clipboard successfully 👍