Search code examples
pythondictionarycollectionsmaxmin

Get maximum and minimum values on dict with list values


The dict is

mydict = {'A': [10, 9, 8], 'B': [2,3,4,5]})

Output needed

Max A=10, Min A=8
Max B=5, Min B=2

Is there any way to do it? I am using defaultdict from collections and appending the values in the middle of the program so I don't know the key and values until the block gets completed.


Solution

  • You can do it with items:

    for k,v in mydict.items():
        print(f'Max {k}={max(v)}, Min {k} ={min(v)}')