Get key by value, dict, python

How can I get the key to the 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-functional code:

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

expected output:

 Normal UK project 

actual conclusion:

 French project UK Omnibus project German project Normal UK project 

How can I fix my code?

+8
python dictionary key-value
source share
7 answers

The problem is that the types of values ​​in the dictionary do not match, which makes it difficult to use the dictionary not only in this scenario. Although Python allows this, you really should consider combining types in a dictionary, for example. make all the lists. You can do this in only one line of code:

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

Now each individual line is wrapped in a list, and your existing code will work correctly.

Alternatively, if you need to use the dictionary in the current form, you can adapt your search function:

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

c={} - any voice recorder
a (value) - you need to know this key

 key=list(c.keys())[list(c.values()).index(a)]] 
+6
source share
 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 cut it a little, I could 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" 
+3
source share

Your semi-functional code returns other values ​​because records such as:

  "Normal UK project" : "1", 

.. then "1" in v checks if the string contains the character "1", while with elements of the type:

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

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

The in operator works both in rows and in lists, but in a different way:

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

Ideally, your data will be more consistent - all lists or all rows:

 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 
+2
source share

The following code provides a different and short version of getting keys with dictionaries using some value using list methods and dic.items ():

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

I personally consider using in , and the values function is simpler.

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

Exit:

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

All Articles