user9198426
user9198426

Reputation:

How to print the value of a specific key 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.

Upvotes: 26

Views: 267501

Answers (10)

shahrukhjaved786
shahrukhjaved786

Reputation: 9

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
}

list_keys = []
list_values = []

for value in fruit.keys():
    list_keys.append(value)

for value in fruit.values():
    list_values.append(value)

print(list_keys)
print(list_values)

for value_ipos,value_item in enumerate(list_keys):
    if value_item == "kiwi":
        print(value_item,list_values[value_ipos])

Upvotes: -1

Jonathan
Jonathan

Reputation: 37

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]])

Upvotes: 2

Joshua Obiero
Joshua Obiero

Reputation: 1

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']))

Upvotes: -2

deadshot
deadshot

Reputation: 9051

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

Upvotes: 20

MOHANPAL SINGH
MOHANPAL SINGH

Reputation: 11

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)

Upvotes: 1

ColonelFazackerley
ColonelFazackerley

Reputation: 892

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

Upvotes: -2

Joe Iddon
Joe Iddon

Reputation: 20414

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

Upvotes: 30

Alan Hoover
Alan Hoover

Reputation: 1440

You can access the value of key 'kiwi' with

print(fruit['kiwi'])

Upvotes: 2

arundeepak
arundeepak

Reputation: 594

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.

Upvotes: 16

Abhisek Roy
Abhisek Roy

Reputation: 584

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")

Upvotes: 1

Related Questions