Home > other >  Finding minimum average value
Finding minimum average value

Time:11-19

So I have a dictionary with 3 keys and for each key its value is a list of three numbers. I wanted to know how to use the built in min function to find the key with the lowest average number. my list looks like this:

{'Tyler': [12.0, 50.0, 450.0],
 'Wesley': [100.0, 120.0, 900.0, 95.0],
 'Tim': [8.0, 150.0, 150.0]}

CodePudding user response:

Or you can use sum and len:

min(d, key=lambda x: sum(d[x])/len(d[x]))

Output:

Tim

CodePudding user response:

Use:

from statistics import mean
dic = {'Tyler': [12.0, 50.0, 450.0],
       'Wesley': [100.0, 120.0, 900.0, 95.0],
       'Tim': [8.0, 150.0, 150.0]}
min(dic.items(), key=lambda x: mean(x[1]))[0]

Output: 'Tim'

Or manually computing the mean:

min(dic.items(), key=lambda x: sum(x[1])/len(x[1]))[0]
  • Related