dict = {'ENSTRUT00000047813': '1680', 'ENSTRUT00000047812': '2067', 'ENSTRUT00000047811': '2067', 'ENSTRUT00000047810': '2088', 'ENSTRUT00000047814': '738', 'ENSTRUT00000047808': '2208', 'ENSTRUT00000047809': '2001'}

I want to find out the key with max value from the dict

I am using the

protein = max(dict.iteritems(),key=operator.itemgetter(1))[0]

but it will give me results as

'ENSTRUT00000047814'

though i know that it sort the values in the dict and then give the max, but eventually it report '738' bigger than any other values starting from either '1' or '2' like 1680 or 2067 respectively.

Recommended Answers

All 2 Replies

protein = max(dict.values(), key=int)

print protein

and

proteins = max(dict.iteritems(), key = lambda x:int(x[1]))[0]
# dict is not good name for a dict as it shadows the builtin type
mydict = {'ENSTRUT00000047813': '1680',
          'ENSTRUT00000047812': '2067',
          'ENSTRUT00000047811': '2067',
          'ENSTRUT00000047810': '2088',
          'ENSTRUT00000047814': '738',
          'ENSTRUT00000047808': '2208',
          'ENSTRUT00000047809': '2001'}
max_protein = max(mydict.items(), key = lambda x: int(x[1])) [0]
print max_protein
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.