how to get specific key-value from dictionary 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: how to get value from key dict in python
# Get a value from a dictionary in python from a key
# Create dictionary
dictionary = {1:"Bob", 2:"Alice", 3:"Jack"}
# Retrieve value from key 2
entry = dictionary[2]
# >>> entry
# Alice
Example 3: extract specific key values from python dictionary
test_data = [{'id':1, 'value':'one'}, {'id':2, 'value':'two'}, {'id':3, 'value':'three'}]
generator = ( item['value'] for item in test_data )
...
for i in generator:
do_something(i)