This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
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.
The post How to Find An Element in List in Python appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Ariessa Norramli
Ariessa Norramli | Sciencx (2021-02-22T11:39:22+00:00) How to Find An Element in List in Python. Retrieved from https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.