10-Feb-2023
.
Admin
Hi Dev,
There are several ways to check value is exists or not in dictionary in python. i will give you two examples using if condition and using custom function in python.
Here, I will show you how to check if value exists in python dictionary. I explained simply about python dictionary value exists or not. you can see python check if values exists in dictionary. I explained simply about python check value in dictionary exists. Alright, let’s dive into the steps.
Example 1:
main.py
user = {
"ID": 1,
"name": "Piyush",
"email": "piyushk@gmail.com"
}
# Check value is present in dictionary
if "Hardik" in user.values():
print("value is exists.")
else:
print("value is not exists.")
Output:
value is exists.
Example 2:
main.py
user = {
"ID": 1,
"name": "Piyush",
"email": "piyush@gmail.com"
}
def checkValueExists(dic, key):
if key in dic.values():
print("Present")
else:
print("Not present")
# Check value is present in dictionary
value = 'Piyush'
checkValueExists(user, value)
# Check value is present in dictionary
value = 'w'
checkValueExists(user, value)
Output:
Present
Not present
#Python