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 Janakpur Dham, Nepal. He was born on December 23, 1994, 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. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment