How to print Specific key value from a dictionary?

Question:

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.

Asked By: user9198426

||

Answers:

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)
Answered By: Abhisek Roy

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
Answered By: Joe Iddon

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")
Answered By: Abhisek Roy

You can access the value of key ‘kiwi’ with

print(fruit['kiwi'])
Answered By: Alan Hoover
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.

Answered By: arundeepak
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

  1. This will get slow with a large dict
  2. only first match will be returned
  3. your value is a float, so a tolerance is needed: I picked 0.01
Answered By: ColonelFazackerley

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)

Answered By: MOHANPAL SINGH

It’s too late but none of the answer mentioned about dict.get() method

>>> print(fruit.get('kiwi'))
2.0

In dict.get() method you can also pass default value if key not exist in the dictionary it will return default value. If default value is not specified then it will return None.

>>> print(fruit.get('cherry', 99))
99

fruit dictionary doesn’t have key named cherry so dict.get() method returns default value 99

Answered By: deadshot
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’]))

Answered By: Joshua Obiero

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]])
Answered By: Jonathan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.