CBSE Class 11 Computer Science Question 83 of 112

Dictionaries — Question 5

Back to all questions
5
Question

Question 5

Why is following code not giving correct output even when 25 is a member of the dictionary?

dic1 = {'age': 25, 'name': 'xyz', 'salary': 23450.5}  
val = dic1['age']  
if val in dic1:  
  print("This is member of the dictionary")  
else :  
  print("This is not a member of the dictionary")  
Answer

The code is not giving the desired output because 25 is present in dic1 as a value and not as a key. "val in dic1" only checks val in the keys of dictionaries.
We can get the desired output by changing val in dic1 to val in dic1.values().