Last modified: February 06, 2021

Check if a List has an Item in Python

In this post, we'll write a good and bad way that check if a list has items.

check if a list has item [Good way]

    
#check if the list has item

#list
list_1 = ("item_1", "item_2", "item_3")

#check if list is not empty
if list_1:
    print("Yes! the list has items")
    

output

Yes! the list has items

as you can see, the code detects that list has items

check if a list has item [Bad way]

    
#check if the list has item

#list
list_1 = ("item_1", "item_2", "item_3")

#check if list is not empty
if len(list_1) > 0:
    print("Yes! the list has items")
    

as you can see, the code detect that list has items

output

Yes! the list has items

Oh!, it's working, but this is the most common mistake that Python developers make. So, if you wanna check if the list has items or not, you should do like the first example.