'How to print Specific key value from a dictionary?
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
print(value)
I want to print the kiwi key, how?
print(value[2])
This is not working.
Solution 1:[1]
Python's dictionaries have no order, so indexing like you are suggesting (fruits[2]) makes no sense as you can't retrieve the second element of something that has no order. They are merely sets of key:value pairs.
To retrieve the value at key: 'kiwi', simply do: fruit['kiwi']. This is the most fundamental way to access the value of a certain key. See the documentation for further clarification.
And passing that into a print() call would actually give you an output:
print(fruit['kiwi'])
#2.0
Note how the 2.00 is reduced to 2.0, this is because superfluous zeroes are removed.
Finally, if you want to use a for-loop (don't know why you would, they are significantly more inefficient in this case (O(n) vs O(1) for straight lookup)) then you can do the following:
for k, v in fruit.items():
if k == 'kiwi':
print(v)
#2.0
Solution 2:[2]
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
if value == 2.00:
print(key)
I think you are looking for something like this.
Solution 3:[3]
You can access the value of key 'kiwi' with
print(fruit['kiwi'])
Solution 4:[4]
EASY PEASY LEMON SQUEEZY You can access the key without knowing the name of the key just with the index of a list. Make all the keys a list and then look for the index number you want of the keys.
tell_me_why = {
'You': 56,
'Are': 23,
'My': 43,
'Fire': 78,
'The': 11,
"One":10,
'Desire':8,
'Belive':6,
'When':134,
'I':1234,
'Say':77,
"I Want":123,
'It':12345,
"That":123211,
'Way':12345
}
#make the keys a list
tell_me_why_keys = list(tell_me_why.keys())
#print the list of the keys
print(tell_me_why_keys)
#print certain key
print(tell_me_why_keys[5])
#print certain key value
print(tell_me_why[tell_me_why_keys[5]])
Solution 5:[5]
Is this what you are looking for?
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key in fruit:
print(fruit[key],"=",key)
Solution 6:[6]
If you only want to display the Kiwi field.
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
print(fruit["kiwi"],"=kiwi")
Solution 7:[7]
You can simply print by using below command
print(fruit['kiwi'])
If you want to check whether a key has been successfully added or not, use below command:
print('key' in dictionary)
for eg:
print('New york' in USA)
Solution 8:[8]
def reverse_dict(dictionary, lookup_val):
for key,value in fruit.items():
if abs(lookup_val-value) < 0.01:
return key
fruit = {
"banana": 1.00,
"apple": 1.53,
"kiwi": 2.00,
"avocado": 3.23,
"mango": 2.33,
"pineapple": 1.44,
"strawberries": 1.95,
"melon": 2.34,
"grapes": 0.98
}
for key,value in fruit.items():
print("{}:{}".format(key, value))
print(fruit['kiwi'])
print(reverse_dict(fruit, 2.00))
Many reasons for caution
- This will get slow with a large dict
- only first match will be returned
- your value is a float, so a tolerance is needed: I picked 0.01
Solution 9:[9]
favorite_numbers = {'jacob': 5, 'christine': 12, 'opiyo': 13, 'jeremy': 27, 'dingo': 36}
print("Here is each individual's favorite number:")
print('jacob'.title() + ": " + str(favorite_numbers['jacob'])) print('christine'.title() + ": " + str(favorite_numbers['christine']))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
