Instagram
youtube
Facebook
Twitter

Python Program to Check if a Value Exists in a Dictionary

A Python Program to Check if a Value Exists in a Dictionary?

Code Explanation:

Dictionary Creation:
A dictionary my_dict is defined with key-value pairs such as name, age, and city.

Value to Check:
A variable value_to_check is initialized with the value you want to find, here it is "London".

Value Existence Check:
if value_to_check in my_dict.values(): is used to check if the given value is present in any of the dictionary values.

Conditional Output:
If the value exists, it prints "Value exists in the dictionary."
Otherwise, it prints "Value does not exist in the dictionary."

 

Program:

my_dict = {

    "name": "Alice",

    "age": 30,

    "city": "London"

}

value_to_check = "London"

if value_to_check in my_dict.values():

    print("Value exists in the dictionary.")

else:

    print("Value does not exist in the dictionary.")