search key in dictionary 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: how to know if a key is in a dictionary python
dict = {"key1": 1, "key2": 2}
if "key1" in dict:
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!")