19-Jan-2023
.
Admin
Hi Dev,
Are you looking for example of check if key exists in python list example. you can see how to check if key exists in python list. I’m going to show you about python check if key exists list. This article goes in detailed on check if key exists in list python. Alright, let’s dive into the steps.
If you want to check key exists or not in the python list then I will give you the following examples. we will use "in" with list for checking key exist or not.
Example 1:
main.py
# Created new List
myList=[ 1, 2, 3, 4, 5, 6 ]
# Key for check id exist or not
key=3
# Checking key exist or not in myList var
if key in myList:
print("Given key is exist in List.")
else:
print("Given key is not exist in List.")
Output:
Given key 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 key is exist in List.")
Output:
Given key 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 key is exist in List.")
else:
print("Given key is not exist in List.")
Output:
Given key is exist in List.
#Python