Check Element in Python List: Using ‘in’ Operator

In Python, you can use the “in” keyword to check if an element exists in a list. Here is an example code:

my_list = [1, 2, 3, 4, 5]

if 3 in my_list:
    print("3 is in the list")
else:
    print("3 is not in the list")

In the code above, we first define a list called my_list containing some numbers, and then use the keyword ‘in’ to check if the number 3 is in the list. If 3 is in the list, it will print “3 is in the list”, otherwise it will print “3 is not in the list”.

bannerAds