Checking if a value exists in a dictionary allows you to verify whether a specific value is present as a value in the dictionary. In this blog post, we will explore different methods to check if a value exists in a dictionary in Python.
Method 1: Using the in Keyword
# Method 1: Using the in Keyword my_dict = {"name": "John", "age": 30, "city": "New York"} value = "John" if value in my_dict.values(): print("Value exists in the dictionary") else: print("Value does not exist in the dictionary")
Output:
Value exists in the dictionary
Method 2: Using the values() Method
# Method 2: Using the values() Method my_dict = {"name": "John", "age": 30, "city": "New York"} value = "John" if value in my_dict.values(): print("Value exists in the dictionary") else: print("Value does not exist in the dictionary")
Output:
Value exists in the dictionary