Computer Science

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")  

Python Dictionaries

18 Likes

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().

Answered By

11 Likes


Related Questions