Dictionaries are disordered. To create an ordered dictionary, use collections.OrderedDictas follows:
from collections import OrderedDict
d = {'a': [2,4,5], 'b': [4,6,7], 'c': [3,1,1]}
modified = {k: max(v) for k, v in d.items()}
answer = OrderedDict(sorted(modified.items(), key=lambda x: x[1], reverse=True))
print(answer)
Exit
OrderedDict([('b', 7), ('a', 5), ('c', 3)])
Then you can easily iterate over the ordered dictionary as follows:
for k, v in answer.items():
print('{} : {}'.format(k, v))
Exit
b : 7
a : 5
c : 3
source
share