How to Check If Element is Present in List Python?

By Hardik Savani October 30, 2023 Category : Python

Hello,

I am going to show you an example of how to check if element is present in list python. This post will give you a simple example of how to check if element is not present in list python. I would like to share with you how to find if an element is present in a list in python. I’m going to show you about python check if element in list exists. you will do the following things for python check if value in list exists.

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.

Without any further ado, let's see the code examples below.

You can use these examples with python3 (Python 3) version.

Example 1:

main.py

# Created new List
myList=[ "Hardik", "Vimal", "Harshad" ]
  
# 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.

I hope it can help you...

Tags :
Shares