You can use in operator in Python to check if a key or value exists in Python dictionary as shown in the below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
dict = {"name": "debjeet", "age": 35} ## Check if a key exists if "name" in dict: print(True) else: print(False) if "sex" in dict: print(True) else: print(False) ## Check if a value exists if "debjeet" in dict.values(): print(True) else: print(False) if "chandrima" in dict.values(): print(True) else: print(False) ## Returns ## True ## False ## True ## False |