Python Dictionary check key and value exist or not

How to check if a value exists in a dictionary (python)

Now we will see the example to check key and value exists in the dictionary or not.

Example: 1

user_info = {'name': 'Pramod', 'Rollno': '17691a05b9', 'Branch': 'CSE'}

if 'name' in user_info:
    print("prensent")
else:
    print("not present")

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe
C: / Users / Pramod / PycharmProjects / pythonProject / main.py
prensent

Process
finished
with exit code 0

Example: 2

if 22 in user_info.values():
    print('exis')
else:
    print('not exist')

Output:

C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
not exist

Process finished with exit code 0

Recommended Post:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpurdham, Nepal. He was born on December 23, 1996, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India.He is director of Appan Bazaar Pvt.Ltd., as teacher in Tridev Eng. School , and an Educational Consultant, and he is currently working as a developer and Digital Marketer.



Leave a Comment