How to check if value is in dictionary python – Python: Check if a Value Exists in the Dictionary

How to check if value is in dictionary python: In this article, we will be discussing how to check if a value exists in the python dictionary or not.

Before going to the different method to check let take a quick view of python dictionary and some of its basic function that we will be used in different methods.

Dictionary in python is an unordered collection of elements. Each element consists of key-value pair.It must be noted that keys in dictionary must be unique because if keys will not unique we might get different values in the same keys which is wrong. There is no such restriction in the case of values.

For example: {“a”:1, “b”:2, “c”:3, “d”,4}

Here a,b,c, and d are keys, and 1,2,3, and 4 are values.

Now let take a quick look at some basic method that we are going to use.

  1. keys():  The keys() method in Python Dictionary, return a list of keys that are present in a particular dictionary.
  2. values(): The values() method in Python Dictionary, return a list of values that are present in a particular dictionary.
  3. items(): The items() method in python dictionary return a list of key-value pair.

syntax for using keys() method: dictionary_name.keys()

syntax for using values() method: dictionary_name.values()

syntax for using items() method: dictionary_name.items()

Let us understand this with basic code

d={"a":1,
    "b":2,
    "c":3,
    "d":4
    }
print(type(d))
print(d.keys())
print(d.values())
print(d.items())

Output

<class 'dict'>
dict_keys(['a', 'b', 'c', 'd'])
dict_values([1, 2, 3, 4])
dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

Here we see that d is our dictionary and has a,b,c, and d as keys and 1,2,3,4 as values and items() return a tuple of key-value pair.

Different methods to check if a value exists in the dictionary

  • Using in operator and values()

    As we see values() method gives us a list of values in the python dictionary and in keyword is used to check if a value is present in a sequence (list, range, string, etc.).So by using the combination of both in operator and values() method we can easily check if a value exists in the dictionary or not. This method will return boolean values i.e. True or False. True if we get the value otherwise False.

d={"a":1,
    "b":2,
    "c":3,
    "d":4
    }
print(3 in d.values())
if(3 in d.values()):
    print("3 is present in dictionary")
else:
    print("3 is not present in dictionary")

Output

True
3 is present in dictionary
  • Using for loop

Python check value in dictionary: Here we can use for loop in different ways to check if a value exists in the dictionary or not. One of the methods is to iterate over the dictionary and check for each key if our value is matching or not using a conditional statement(if-else). And another method is that we can iterate over all the key-value pairs of dictionary using a for loop and while iteration we can check if our value matches any value in the key-value pairs.

Let us see both the method one by one.

First method

d={"a":1,
    "b":2,
    "c":3,
    "d":4
    }
res = False
for key in d:
    if(d[key] == 3):
        res = True 
        break
if(res):
     print("3 is present in dictionary")
else:
     print("3 is not present in dictionary")

Output

3 is present in dictionary

Here we iterate over the dictionary and check for each key if our value is matching or not using a conditional statement(if-else).

Second Method

d={"a":1,
    "b":2,
    "c":3,
    "d":4
    }
res = False
value=3
for key, val in d.items():
    if val == value:
        res = True
        break
if(res):
    print(f"{value} is present in dictionary")
else:
    print(f"{value} is not present in dictionary")

Output

3 is present in dictionary

Here we can iterate over all the key-value pairs of dictionary using a for loop and while iteration we can check if our value matches any value in the key-value pairs.

  • Using any() and List comprehension

Check if dict key exists python: Using list comprehension, iterate over a sequence of all the key-value pairs in the dictionary and create a bool list. The list will contain a True for each occurrence of our value in the dictionary. Then call any() function on the list of bools to check if it contains any True. any() function accept list, tuples, or dictionary as an argument. If yes then it means that our value exists in any key-value pair of the dictionary otherwise not.

d={"a":1,
    "b":2,
    "c":3,
    "d":4
    }
value=3
if any([True for k,v in d.items() if v == value]):
    print(f"Yes, Value: '{value}' exists in dictionary")
else:
    print(f"No, Value: '{value}' does not exists in dictionary")

Output

Yes, Value: '3' exists in dictionary

So these are the different methods to check if a value exists in the dictionary or not.