Home > OS >  Get all keys with the same value in a dictionary
Get all keys with the same value in a dictionary

Time:02-07

I have a dictionary with all the letters and I want to get all the letters (so the keys) that occur the most.

letters = {'A': 0, 'B': 0, 'C': 0, 'D': 0, 'E': 1, 'F': 1, 'G': 1, 'H': 0, 'I': 0, 'J': 0, 'K': 0, 'L': 2, 'M': 4, 'N': 0, 'O': 1, 'P': 1, 'Q': 4, 'R': 0, 'S': 1, 'T': 2, 'U': 2, 'V': 0, 'W': 0, 'X': 1, 'Y': 0, 'Z': 0}

So M and Q are to be output

My Python version is 3.9.2

CodePudding user response:

max_value = max(letters.values())
[key for key, val in letters.items() if val == max_value]
  •  Tags:  
  • Related