How to Check a List is Empty in Python

An empty list in Python is equal to boolean False. We can test this using the Python boolean() function like this:


items = []



If a list is Empty Python Code

With that knowledge, we can determine to do something depending on whether a list is empty has no value or not using an if else statement like this:


items = []

if items:
   print('List contains items do something here...')
   print('List is empty do something here...')
List is empty do something here...


The above code says if True the list isn't empty so run some code in the block else the list must be empty.


Check a list is Empty with Python if not

We can reverse the polarity of the previous example using the Python not keyword like this:


items = []

if not items:
   print('List is empty')
List is empty


The above code says if the supplied value is not True, run what is inside the block. The advantage of this is not having to use an else block in the case of an empty list.

Related Tutorials


Python Assert Statement

September 30, 2020

How to use Sets in Python

September 15, 2020