Using both
values() method and ‘in’ operator, we can check for the existence of a value in
a Dictionary.
Retrieve the values from the dictionary:
Use the values() method to obtain a view object containing all the values stored in the dictionary.
Use the in operator
Apply the in operator to this view object to find whether the given value is present within it or not.
check_for_value_existence.py
employee = {'id' : 123, 'name' : 'Krishna', 'age' : 34}
if 'krishna' in employee.values():
print('Krishna is in employee')
else:
print('Krishna is not in employee')
if 'Ram' in employee:
print('Ram is in employee')
else:
print('Ram is not in employee')
Output
Krishna is not in employee Ram is not in employee
Previous Next Home
No comments:
Post a Comment