0

If I have this dictionary how can I do to get the key instead value but reading words. Example if I write 2 22 1 response must be A B .

value= {"A": 2, "B": 22, "C": 222, "D": 3, "E": 33,"F":333, "G": 4, "H": 44, "I": 444, "J": 5, "K": 55, "L": 555, "M": 6, "N": 66, "O": 666, "P": 7, "Q": 77, "R": 777, "S": 7777, "T": 8, "U": 88, "V": 888, "W": 9, "X": 99, "Y": 999, "Z": 9999}

I was trying with this loop but there´s something wrong:

for i in r:
  key= n[i]
  print key
apk
  • 37
  • 11

1 Answers1

0

You can reverse the dictionary:

value= {"A": 2, "B": 22, "C": 222, "D": 3, "E": 33,"F":333, "G": 4, "H": 44, "I": 444, "J": 5, "K": 55, "L": 555, "M": 6, "N": 66, "O": 666, "P": 7, "Q": 77, "R": 777, "S": 7777, "T": 8, "U": 88, "V": 888, "W": 9, "X": 99, "Y": 999, "Z": 9999} 
new_value = {b:a for a, b in value.items()}
print(new_value[2])

Output:

A

Regarding your latest comment, you can try this:

final_results = [a for a, b in value.items() if b == 22]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102