how to find key of a value in a dict in python code example
Example 1: how to get key of a particular value in dictionary python using index
mydict = {'george': 16, 'amber': 19}
print(list(mydict.keys())[list(mydict.values()).index(16)]) # Prints george
Example 2: find a key in a dictionary python
# How to find a key / check if a key is in a dict
# Easiest way
fruits = {'apple': 2, 'pear': 5, 'strawberry': 3}
if 'apple' in fruits:
print("Key found!")
else:
print("Key not found!")