10

How can I get a key from a value?

my dict:

countries = { "Normal UK project" : "1", "UK Omnibus project" : "1-Omni", "Nordic project" : ["11","12","13","14"], "German project" : "21", "French project" : "31" } 

my semi functioning code:

for k, v in countries.items(): if "1" in v: print k 

expected output:

Normal UK project 

actual output:

French project UK Omnibus project German project Normal UK project 

How can I fix my code?

9
  • 6
    if "1" == v:? Commented Apr 25, 2014 at 14:01
  • if the value I am looking for is "12" then it would fail Commented Apr 25, 2014 at 14:02
  • 3
    First, you should unify the type of your values. Some are strings, some are lists. Better use only lists (some with just one element), then your code would actually work. Commented Apr 25, 2014 at 14:02
  • 3
    If you often find yourself trying to get the key from the value, perhaps the dictionary is the wrong way around? Commented Apr 25, 2014 at 14:03
  • 1
    I think this is a duplicate of stackoverflow.com/questions/8023306/… Commented Apr 25, 2014 at 14:04

7 Answers 7

8

c={} - any dict
a(value) - need know this key

key=list(c.keys())[list(c.values()).index(a)] 
Sign up to request clarification or add additional context in comments.

1 Comment

This works but .index() will only return the first instance of a in c
7

The problem is that the types of the values in the dictionary are not the same, making it much more difficult to use the dictionary, not only in this scenario. While Python allows this, you really should consider unifying the types in the dictionary, e.g. make them all lists. You can do so in just one line of code:

countries = {key: val if isinstance(val, list) else [val] for key, val in countries.items()} 

Now, each single string is wrapped into a list and your existing code will work correctly.

Alternatively, if you have to use the dictionary in it's current form, you can adapt your lookup function:

for k, v in countries.items(): if "1" == v or isinstance(v, list) and "1" in v: print k 

Comments

4

The following code provide another and short version of getting dictionary keys by some value, by using list comprehensions and dic.items():

keys_have_value = [k for k,v in dic.items() if v=="1"] 

1 Comment

Code-only answers are discouraged as they show poor attempt to answer the question.
3
def keys_of_value(dct, value): for k in dct: if isinstance(dct[k], list): if value in dct[k]: return k else: if value == dct[k]: return k assert keys_of_value(countries, "12") == "Nordic project" assert keys_of_value(countries, "1") == "Normal UK project" 

If you want me to shorten it a little bit, I might do

from operator import eq, contains def keys_of_value(dct, value, ops = (eq, contains)): for k in dct: if ops[isinstance(dct[k], list)](dct[k], value): return k assert keys_of_value(countries, "12") == "Nordic project" assert keys_of_value(countries, "1") == "Normal UK project" 

1 Comment

Thanks for the effort much appreciated.
2

Your semi-functioning code is returning the other values because with entries like:

 "Normal UK project" : "1", 

..then "1" in v checks if the string contains the "1" character, whereas with entries like:

 "Nordic project" : ["11","12","13","14"], 

..then it will check if the list contains an element "1".

The in operator works on both strings and lists, but in a different manner:

>>> "1" in "123" True >>> "1" in ["123", "blah"] False >>> "1" in ["1", "blah"] True 

Ideally your data would be more consistent - all lists, or all strings:

countries = { "Normal UK project" : ["1"], "UK Omnibus project" : ["1-Omni"], "Nordic project" : ["11","12","13","14"], "German project" : ["21"], "French project" : ["31"] } for k, v in countries.items(): if "1" in v: print k 

1 Comment

I was going to do this but I was not sure if that was the right thing to do as it seemed a bit redundant to have a list of one item.
0

I personally think using in and the function values is easier.

print 1 in {1:"123", 2:"blah"} print "blah" in {1:"123", 2:"blah"}.values() 

Output is:

True True 

Comments

0
def get_Value(dic,value): for name in dic: if dic[name] == value: return name 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.