Signup/Sign In
PUBLISHED ON: AUGUST 26, 2021

How to check if value exists in dictionary?

In this tutorial, we will learn how to check if a value exists in the dictionary.

In the Python programming language, List, Tuple, and Set represent a group of individual objects as a single entity. If we want to represent a group of objects as key-value pairs then we should go for the Dictionary concept.

We can check whether a value exists or not by getting values from the dictionary using the values(), items(), get(), by accessing the keys of the dictionary and using the membership operator.

Example: Using values() method

The below example shows how to get values from the built-in method values().

dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"} 
x=dict_1.values()
print("values present in dictionary dict_1 are:",x)
print("The type of x is:",type(x))
# we can iterate keys values:
for k in dict_1.values(): 
    print("The value present in dictionary dict_1 is:",k)

Once we run the code, it shows the following result.


values present in dictionary dict_1 are: dict_values(['python', 'Java', 'Ruby', 'C', 'C++', 'R'])
The type of x is: <class 'dict_values'>
The value present in dictionary dict_1 is: python
The value present in dictionary dict_1 is: Java
The value present in dictionary dict_1 is: Ruby
The value present in dictionary dict_1 is: C
The value present in dictionary dict_1 is: C++
The value present in dictionary dict_1 is: R

Example: Using items() method

The below example shows how to get values from the built-in method items().

#dictionary with key value pairs
dict_1={100:"python",200:"Java",300:"Ruby"} 
# Getting keys using items() method
x=dict_1.items()
print(x)
for k,v in x: 
    print("From the dict_1,the value element is:",v) 
  

Once we run the code, it shows the following result.


dict_items([(100, 'python'), (200, 'Java'), (300, 'Ruby')])
From the dict_1, the value element is: python
From the dict_1, the value element is: Java
From the dict_1, the value element is: Ruby

Example: Using get() method

The below example shows how to get values from the built-in method items(). If the specified key is not present in the dictionary, it returns none.

#dictionary with key value pairs
dict_1={100:"python",200:"Java",300:"Ruby"} 
# Getting keys using get() method
x=dict_1.get(100,"Python")
print(x)
y=dict_1.get(100)
print(y)
z=dict_1.get("Java")
print(z)
a=dict_1.get("R")
print(a)

Once we run the code, it shows the following result.


python
python
None
None

Example: We can get values directly by accessing keys

The below example shows how to get values directly by accessing keys.

#dictionary with key value pairs
dict_1={100:"python",200:"Java",300:"Ruby"}
print("The values are:")
print(dict_1[100])
print(dict_1[200])
print(dict_1[300])

Once we run the code, it shows the following result.


The values are:
python
Java
Ruby

Following are the other methods to check if a specific value exists in a dictionary or not.

Example: Using Membership operator

From the previous example we learned how to get values from the dictionary using the values() method. In the same way, we can check if the value is present in a dictionary or not.

In the below example, we are taking the input from the user to check value is in a dictionary or not.

Using the values() method in the if statement, we are getting the values, and using the membership operator we are checking the value from the user input is in a dictionary or not.

value=input("Enter the value element to be check:")
print("The value element to be checked is:",value)
dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"} 
if value in dict_1.values():
    print("The specifed value is present in dictionary")
else:
    print("The specified value is not present in dictionary")

Once we run the code, it shows the following result.


Enter the value element to be check: python
The value element to be checked is: python
The specified value is present in the dictionary

Example: Using the items() method

The below example is similar to the previous example. Instead of the value() method, we are using the item() method to get the values.

We can check values present in a dictionary or not using the items() method.

value=input("Enter the value element to be check:")
print("The value element to be checked is:",value)
dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"} 
for k,v in dict_1.items():
    if v==value:
        print("The specifed value is present in dictionary")
    

Once we run the code, it shows the following result.


Enter the value element to be check: Java
The value element to be checked is: Java
The specified value is present in the dictionary

Conclusion

In this tutorial, we learned how to get values from the dictionary from the built-in functions and how to check if a specific key exists in a dictionary or not.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.