checking if a list contains an item python [Good way]
- Last modified: 26 August 2020
- Category: python tutorial
In this post, we'll write a good and bad way that check if a list has items.
check if a list contains item [Good way]
#check if the list contains item
#list
list_1 = ("item_1", "item_2", "item_3")
#check if list is not empty
if list_1:
print("Yes! the list contains items")
output
Yes! the list contains items
as you can see, the code detects that list has items
check if a list contains item [Bad way]
#check if the list contains 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 contains items")
as you can see, the code detect that list has items
output
Yes! the list contains items
Oh!, it's working, but this is the most common mistake that Python developers make. So, if you wanna check if the list contains items or not, you should do like the first example.
English today is not an art to be mastered it's just a tool to use to get a result