8

For example lets say we have the following dictionary:

dictionary = {'A':4,
              'B':6,
              'C':-2,
              'D':-8}

How can you print a certain key given its value?

print(dictionary.get('A')) #This will print 4

How can you do it backwards? i.e. instead of getting a value by referencing the key, getting a key by referencing the value.

Robbie
  • 213
  • 2
  • 10
Jett
  • 2,421
  • 5
  • 16
  • 14

7 Answers7

21

I don't believe there is a way to do it. It's not how a dictionary is intended to be used... Instead, you'll have to do something similar to this.

for key, value in dictionary.items():
    if 4 == value:
        print key
SlxS
  • 424
  • 3
  • 8
2

In Python 3:

# A simple dictionary
x = {'X':"yes", 'Y':"no", 'Z':"ok"}

# To print a specific key (for instance the 2nd key which is at position 1)
print([key for key in x.keys()][1])

Output:

Y
Fouad Boukredine
  • 1,495
  • 14
  • 18
1

The dictionary is organized by: key -> value

If you try to go: value -> key

Then you have a few problems; duplicates, and also sometimes a dictionary holds large (or unhashable) objects which you would not want to have as a key.


However, if you still want to do this, you can do so easily by iterating over the dicts keys and values and matching them as follows:

def method(dict, value):
    for k, v in dict.iteritems():
        if v == value:
            yield k
# this is an iterator, example:
>>> d = {'a':1, 'b':2}
>>> for r in method(d, 2):
    print r

b

As noted in a comment, the whole thing can be written as a generator expression:

def method(dict, value):
    return (k for k,v in dict.iteritems() if v == value)

Python versions note: in Python 3+ you can use dict.items() instead of dict.iteritems()

Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
0
target_key = 4
for i in dictionary:
    if dictionary[i]==target_key:
        print(i)
Emadpres
  • 3,466
  • 2
  • 29
  • 44
boop
  • 1
0

Within a dictionary if you have to find the KEY for the highest VALUE please do the following :

  1. Step 1: Extract all the VALUES into a list and find the Max of list
  2. Step 2: Find the KEY for the particular VALUE from Step 1

The visual analyzer of this code is available in this link : LINK

    dictionary = {'A':4,
              'B':6,
              'C':-2,
              'D':-8}
lis=dictionary.values()
print(max(lis))
for key,val in dictionary.items() :
    if val == max(lis) :
        print("The highest KEY in the dictionary is ",key)
  • The question is asking to do the reverse lookup in general, not to find the key corresponding to the highest value. – fiveclubs May 07 '20 at 19:16
0

I think this is way easier if you use the position of that value within the dictionary.

dictionary = {'A':4,
              'B':6,
              'C':-2,
              'D':-8}
 
# list out keys and values separately
key_list = list(dictionary.keys())
val_list = list(dictionary.values())
 
# print key with val 4
position = val_list.index(4)
print(key_list[position])
 
# print key with val 6
position = val_list.index(6)
print(key_list[position])
 
# one-liner
print(list(my_dict.keys())[list(my_dict.values()).index(6)])
-1

Hey i was stuck on a thing with this for ages, all you have to do is swap the key with the value e.g.

Dictionary = {'Bob':14} 

you would change it to

Dictionary ={1:'Bob'} 

or vice versa to set the key as the value and the value as the key so you can get the thing you want

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96