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. In…

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

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » How to Find An Element in List in Python." Ariessa Norramli | Sciencx - Monday February 22, 2021, https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/
HARVARD
Ariessa Norramli | Sciencx Monday February 22, 2021 » How to Find An Element in List in Python., viewed ,<https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Find An Element in List in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/
CHICAGO
" » How to Find An Element in List in Python." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/
IEEE
" » How to Find An Element in List in Python." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/22/how-to-find-an-element-in-list-in-python/. [Accessed: ]
rf:citation
» How to Find An Element in List in Python | Ariessa Norramli | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.