Last modified: Jan 10, 2023 By Alexander Williams
Python Check if Value Exists in List
In this post, we'll write a good and bad way that check if a list has value.
check if a list has value [Good way]
#check if the list has value
#list
list_1 = ["item_1", "item_2", "item_3", 33]
#check if list has value
if list_1:
print("Yes! the list has value")
output
Yes! the list has value
as you can see, the code detects that list has value
check if a list has value [Bad way]
#list
list_1 = ["item_1", "item_2", "item_3"]
#check if list has value
if len(list_1) > 0:
print("Yes! the list has value")
as you can see, the code detect that list has value
output
Yes! the list has value
Oh!, it's working, but this is the most common mistake that Python developers make. So, if you wanna check if the list has value or not, you should do like the first example.