1d = {"key1": 10, "key2": 23}
2
3if "key1" in d:
4 print("this will execute")
5
6if "nonexistent key" in d:
7 print("this will not")
1# You can use 'in' on a dictionary to check if a key exists
2d = {"key1": 10, "key2": 23}
3"key1" in d
4# Output:
5# True
1>>> d = {'1': 'one', '3': 'three', '2': 'two', '5': 'five', '4': 'four'}
2>>> 'one' in d.values()
3True
1d = {"apples": 1, "banannas": 4}
2# Preferably use .keys() when searching for a key
3if "apples" in d.keys():
4 print(d["apples"])
5