max in an dictionary code example
Example 1: max of a dict
dic={0: 1.4984074067880424, 1: 1.0984074067880423, 2: 1.8984074067880425, 3: 2.2984074067880425, 4: 2.2984074067880425}
max_value = max(dic.values())
max_keys = [k for k, v in dic.items() if v == max_value]
print(max_value, max_keys)
Example 2: get max n values in dict py
import heapq
from operator import itemgetter
n = 3
items = {'a': 7, 'b': 12, 'c': 9, 'd': 0, 'e': 24, 'f': 10, 'g': 24}
topitems = heapq.nlargest(n, items.items(), key=itemgetter(1))
topitemsasdict = dict(topitems)