Home » Check If a List is Empty in python

Check If a List is Empty in python

1 Year Subscription
Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Developer (Upgrade) [1Z0-817]
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
Oracle Java Certification

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