I'm trying to make a function that, for each entry in a dictionary, takes the mean for every entry's list of values and returns the key that corresponds to the lowest mean. If negative values are present anywhere in the list, it should first remove them then compute the means as normally.
For example:
least_mean({'Option1': [0, 1, 2], 'Option2': [8, 9, -9999],'Option3': [0, -9999, 5, 3]})
should return 'Option1' because it has the lowest mean with a value of 1.5
My Attempt
def least_mean(string):
empty = []
for i in string:
empty.append((i,sum([j for j in string[i] if j > 0])/len([j for j in string[i] if j > 0])))
return empty
I have created a function that returns a list of tuples containing each option and their mean. However, I'm not sure how to make this function more specific to return 'Option1' by itself. For example, plugging ({'Option1': [0, 1, 2], 'Option2': [8, 9, -9999],'Option3': [0, -9999, 5, 3]}) in returns
[('Option1', 1.5), ('Option2', 8.5), ('Option3', 4.0)]
but I would like to get 'Option1' alone. If possible, could this be done in a one line list comprehension without imports?