Home ยป Check If a List is Empty in python

Check If a List is Empty in python

In this example we show 3 ways of checking if a list is empty in python

Example

If my_list1 is empty then not returns True

len of my_list2 – If the length of a list is 0, then the list is empty.

if my_list3 has no elements, then it should be equal to [] and is empty

my_list1 = []
my_list2 = []
my_list3 = []

if not my_list1:
    print("the list 1 is empty")
    
if not len(my_list2):
    print("the list 2 is empty")
    
if my_list2 == []:
    print("The list 3 is empty")

When you run this you will see something like this

>>> %Run emptylist.py
the list 1 is empty
the list 2 is empty
The list 3 is empty

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More