Low Orbit Flux Logo 2 D

Python How To Check If List Is Empty

This is almost guaranteed to come up if you have been doing any significant amount of coding. You are going to need to know how to check if a list is empty. As you might expect this is very easy.

To check if a list is empty in Python just use the following:


if not a:
    print("It's empty!!")

That’s it. If the list is empty it will have a value of false and if it isn’t empty it will have a value of true.

You could do this a bit more explicitly if you want by checking the length of the list and comparing that to zero like this.


if len(a) == 0:
    print("This is an empty list!")

Here is another example that isn’t really necessary but we will include it just to be even more complete. This will tell us that the list is empty.


list1 = []

if not list1:
    print("It's empty!!")
else:
    print("It contains data!")