Articles

Program to check if a particular value is present in a dictionary in python

Program to check if a particular value is present in a dictionary in python


In this python programming article, we are going to learn

  • program to check if a particular value is present in a dictionary in python

Program to check if a particular value is present in a dictionary in Python

The program to check if a particular value is present in a dictionary in python is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

subject_marks = {
    "English" : 83,
    "Bengali" : 87,
    "Mathematics" : 98,
    "Physics" : 88,
    "Chemistry" : 91,
    "Computer Science" : 96
}

searchable = int(input("Enter the marks to be searched: ").strip())

for i in subject_marks.values():
    if i == searchable:
        print("You got {} marks in one of the subjects.".format(i))
        break

The output of the program is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the marks to be searched: 98
You got 98 marks in one of the subjects.
PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter the marks to be searched: 55
PS C:\Users\DEVJEET\Desktop\tutorialsInHand>

Few important tips about the program

1. We take the value which the user wants to search.

2. The .values() method of Python dictionary helps us to iterate over the values of the python dictionary.

3. If the value exist, it will print the line. Else, it will not print anything in the console. However, you can use flag variables to handle the situation in a better way if needed.

 

program-to-check-if-a-value-is-present-in-a-python-dictionary

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Data Science Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!