0
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.

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by pyTony
3
protein = max(dict.values(), key=int)

print protein

and

proteins = max(dict.iteritems(), key = lambda x:int(x[1]))[0]

Edited by -ordi-: n/a

1
# 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
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.