how to search a key in dictionary in python code example
Example 1: search in dict python
dictionary = { "key1" : 1, "name" : "Jordan", "age" : 21 }
for key in dictionary.keys():
print("the key is {} and the value is {}".format(key, dictionary[key]))
Example 2: python check if key exists
d = {"key1": 10, "key2": 23}
"key1" in d
Example 3: python check if key exists
d = {"apples": 1, "banannas": 4}
if "apples" in d.keys():
print(d["apples"])
Example 4: find a key in a dictionary python
fruits = {'apple': 2, 'pear': 5, 'strawberry': 3}
if 'apple' in fruits:
print("Key found!")
else:
print("Key not found!")