I need help grabbing the maximum value from a dictionary. I have a dictionary of {num : [state,value] …} and need to grab everything associated with the highest value.
#find max value in dictionary
td = {0: ['b',3], 1: ['b',6], 4: ['b',2], 3: ['b',5] }
#In this example td dict, I want to grab the key and state associated with the highest value, 6. I want to grab "1: ['b',6]"
print td
print td.keys()
print td.values()
maxval = max([v for k,[s,v] in td.iteritems()])
print maxval #correctly prints 6
Just change your
max()comprehension to yield tuples with the value as the first element:So your code might look something like this: