Skip to content

Python if list is empty | Example code

  • by

Use PEP 8 recommended method to check if the list is empty in Python. In the method doing “Truth Value Testing” where check if the list is empty using its boolean value.

Example check if a list is empty in Python

Simple example code using most Pythonic way of checking the empty list. Since an empty list is False, the condition is false and hence we are able to identify an empty list.

l2 = []

if l2:
    print("list is not empty")
else:
    print("list is empty")

Output:

Python if list is empty

Another method is len() function

The len() function returns the length of the argument passed. If the length of the list is zero then the list is empty else not empty.

l2 = [1, 2, 3]

if len(l2):
    print("the list is not empty")
else:
    print("the list is empty")

Output: the list is not empty

Do comment if you have any questions or suggestions on this Python list topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *