Python Dictionary Check if Value Exists Example

By Hardik Savani October 30, 2023 Category : Python

Hey Artisan,

This tutorial will give you an example of python dictionary check if value exists. you'll learn how to check if value is present or not in dictionary python. This tutorial will give you a simple example of python check value in dictionary exists. let us discuss about python check if values exists in dictionary. Let's get started with python dictionary value exists or not.

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.

So, without further ado, let's see simple examples:

Example 1:

main.py

user = {
  "ID": 1,
  "name": "Hardik",
  "email": "hardik@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": "Hardik",
  "email": "hardik@gmail.com"
}

def checkValueExists(dic, key):
    if key in dic.values():
        print("Present")
    else:
        print("Not present")
         
# Check value is present in dictionary
value = 'Hardik'
checkValueExists(user, value)
 
# Check value is present in dictionary
value = 'w'
checkValueExists(user, value)

Output:

Present
Not present

I hope it can help you...

Tags :
Shares