Check if Element is Present in List Python
Hi Dev,
If you need to see example of check if element is present in list python. if you want to see example of python check if value in list exists then you are a right place. This post will give you simple example of python check if element in list exists. I would like to show you how to find if an element is present in a list in python.
If you want to check value exists or not in the python list then I will give you the following examples. we will use "in" with list for checking element exist or not
Example 1:
main.py
# Created new List
myList=[ "Piyush", "Savan", "Raju" ]
# Key for check id exist or not
elem="Hardik"
# Checking elem exist or not in myList var
if elem in myList:
print("Given element is exist in List.")
else:
print("Given element is not exist in List.")
Output:
Given element is exist in List.
Example 2:
main.py
# Created new List
myList=[ 1, 2, 3, 4, 5, 6 ]
# Checking key exist or not in myList var
for i in myList:
if(i == 3):
print("Given element is exist in List.")
Output:
Given element is exist in List.
Example 3:
main.py
# Created new List
myList=[ 1, 2, 3, 4, 5, 6 ]
# Checking key exist or not in myList var
if (3 in myList):
print("Given element is exist in List.")
else:
print("Given element is not exist in List.")
Output:
Given element is exist in List.
- Get Last 2 Digits of Number in Python Tutorial Example
- How to Sum of First and Last Digit of Number in Python?
- How to Get First and Last Digit of Number in Python?
- Get the Last Digit of Number in Python Tutorial Example
- Get the First Digit of Number in Python Tutorial Example
- How to Remove All Decimals from Number in Python?
- How to Convert String to Float with 2 Decimal Places in Python?
- How to Format Number to 2 Decimal Places in Python?