Check if Value Exists in Python Dictionary Last Updated : 10 May, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We are given a dictionary and our task is to check whether a specific value exists in it or not. For example, if we have d = {'a': 1, 'b': 2, 'c': 3} and we want to check if the value 2 exists, the output should be True. Let's explore different ways to perform this check in Python.Naive ApproachThis approach directly checks if the value exists in the dictionary using the in operator with d.values(). Python d = {'a': 10, 'b': 20, 'c': 30} v = 20 if v in d.values(): print(f"The value {v} exists in the dictionary.") else: print(f"The value {v} does not exist in the dictionary.") OutputThe value 20 exists in the dictionary.Explanation: The in operator is used with d.values(), which returns a view of all values in the dictionary.It checks if the given value v exists in the given dictionary.Using a List ComprehensionThis approach uses a list comprehension to create a list of dictionary values and then checks for the value's existence. Python d = {'a': 10, 'b': 20, 'c': 30} v = 20 if v in [v for v in d.values()]: print(f"The value {v} exists in the dictionary.") else: print(f"The value {v} does not exist in the dictionary.") OutputThe value 20 exists in the dictionary.Explanation: A list comprehension iterates over d.values() and creates a list of values.The in operator checks if the value v exists in the list. Using Exception HandlingThis approach uses try and except to handle a ValueError when the value is not found. Python d = {'a': 10, 'b': 20, 'c': 30} v = 20 try: if v in d.values(): print(f"The value {v} exists in the dictionary.") else: raise ValueError except ValueError: print(f"The value {v} does not exist in the dictionary.") OutputThe value 20 exists in the dictionary. Explanation:The try block checks if the value exists in the dictionary.If the value is not found, it raises a ValueError and handles it with the except block.Related Articles:Python Dictionary get() MethodList Comprehension in PythonPython dictionary values()Python Exception Handling Comment More infoAdvertise with us Next Article Get Key from Value in Dictionary - Python S sravankumar_171fa07058 Follow Improve Article Tags : Python Practice Tags : python Similar Reads Get Key from Value in Dictionary - Python The goal is to find the keys that correspond to a particular value. Since dictionaries quickly retrieve values based on keys, there isn't a direct way to look up a key from a value. Using next() with a Generator ExpressionThis is the most efficient when we only need the first matching key. This meth 5 min read Python | Check if given multiple keys exist in a dictionary A dictionary in Python consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. Input : dict[] = {"geeksforgeeks" : 1, "practice" : 2, "contribute" :3} keys[] = {"geeksforgeeks", "practice"} Output : Yes Input : dict[] = {"geeksforgeeks" : 1, "practice" 3 min read How to Check if a Key Exists in a Dictionary in TypeScript ? In TypeScript dictionaries are used whenever the data is needed to be stored in key and value form. We often retrieve the data from the dictionaries using an associated key. Therefore it becomes crucial to check whether the key exists in a dictionary or not. We can use the below methods to check if 4 min read Python - How to Check if a file or directory exists Sometimes it's necessary to verify whether a dictionary or file exists. This is because you might want to make sure the file is available before loading it, or you might want to prevent overwriting an already-existing file. In this tutorial, we will cover an important concept of file handling in Pyt 5 min read Python - Access Dictionary items A dictionary in Python is a useful way to store data in pairs, where each key is connected to a value. To access an item in the dictionary, refer to its key name inside square brackets.Example:Pythona = {"Geeks": 3, "for": 2, "geeks": 1} #Access the value assosiated with "geeks" x = a["geeks"] print 3 min read How to check if a Python variable exists? Checking if a Python variable exists means determining whether a variable has been defined or is available in the current scope. For example, if you try to access a variable that hasn't been assigned a value, Python will raise a NameError. Letâs explore different methods to efficiently check if a va 3 min read Like