python

How to Find An Element in List in Python


In this article, you will learn how to find an element in a list in Python.

Let’s say you have a list of food.

food = ["Pizza", "Burger", "Waffle", "Cake"]

In order to find an element in a list, you can use the in operator. In this example, you will be searching for “Pizza”.

food = ["Pizza", "Burger", "Waffle", "Cake"]

# Set searchFood to "Pizza"
searchFood = "Pizza"

# If searchFood is found in the list
if searchFood in food:

    # Print found message
    print("%s is found in the list" %(searchFood))

# Else, if searchFood is not found in the list
else:

    # Print not found message
    print("%s is not found in the list" %(searchFood))

Note: The in operator functions by checking if a value exists in a sequence or not. If the value exists, it will return true. Otherwise, it will return false.


Share on social media

//