Karyo
Karyo

Reputation: 372

Find all keys with the max same value in python

Hi I have a dictionary like the below:

b = {'tat': 0, 'del': 4, 'galadriel': 0, 'sire': 0, 'caulimovirus': 4, 'retrofit': 0, 'tork': 0, 'caulimoviridae_dom2': 0, 'reina': 4, 'oryco': 2, 'cavemovirus': 1, 'soymovrius': 0, 'badnavirus': 0, 'crm': 0, 'athila': 0}

I want to find all keys with the maximum value as a list. However,

max(a, key=a.get)

only gives the first key element, 'del'.

How should I find all the keys with the maximum values? Like the below.

new_list = ['del', 'caulimovirus', 'reina']

Upvotes: 0

Views: 681

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799180

maxv = max(b.values())
new_list = [k for k, v in b.items() if v == maxv]

Upvotes: 2

Related Questions