I have a list of some random numbers (integers and floats)
In[]: list1
Out[]: [8.6, 9, 15, 20, 1]
I have found out the average/ mean value of the list:
m = reduce(lambda x, y: x + y, list1) / len(list1)
This outputs 10.72 .
Now, I have to find the closest number to avg from the list list1 .
In the above eg, from list1 the closes number to 10.72 is 9.
One way is to find the difference between the avg and each element in the list, and then look for the smallest difference and then output that element producing smallest difference, But I was looking for more crisp and efficient way, please suggest me one.