0

I have sorted a list of dictionary values and am trying to find the keys so I can print them in the order of the values. Here is my code:

list1=[]
dicts={'toy':3,'dino':4,'kite':9,'torch':1}
for key in dicts:
    list1.append(dicts[key])
a=sorted(list1)
print(a)
if a[0]==dicts[key]:
print(dicts.value)

I cannot seem to find the key which corresponds to the value. Can someone help me?

andrewJ
  • 111
  • 2
  • 11
  • something seems very off with your spacing. please fix that – Jason V Jun 22 '17 at 17:49
  • Looking up keys by value will potentially require iterating over the entire dictionary. That's not what dictionaries are used for. Also note that you may have more than one key that goes to a certain value. – Carcigenicate Jun 22 '17 at 17:49
  • check this https://stackoverflow.com/questions/1456373/two-way-reverse-map – Santhosh Tangudu Jun 22 '17 at 17:49
  • Python dictionaries are not bidirectional. There's nothing that enforces that values have to be unique. For example, your toy and dino key can point to the same value. – Adam Hughes Jun 22 '17 at 17:50
  • The question doesn't quite match the title ("look up" vs. "sort"). This might be a better dupe: https://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value – glibdud Jun 22 '17 at 17:56

0 Answers0