# Calculate the average with weight

I have a list:

list = {{17, 2}, {11.55, 3}, {18.5, 1}, {14.2, 1}, {14.25, 2}, {11.5,3}, {15, 2}, {11, 3},{10.8, 3}};

I wish to calculate:

(17*2+11.5*3+18.5*1+14.2*1+14.25*2+...+10.8*3)/(2+3+1+2+...+3)

How can I use list manipulation?

To make @I.M.'s comment explicit:

list = {{17, 2}, {11.55, 3}, {18.5, 1}, {14.2, 1}, {14.25, 2},
{11.5, 3}, {15, 2}, {11, 3}, {10.8, 3}};

data = WeightedData @@ Transpose[list];

Mean[data]
(*    12.9875    *)

Variance[data]
(*    6.53654    *)
• WeightedData is a fruitful function I have not known it!! Thank you so much for your answer. It was so interesting!!! Sep 14, 2021 at 16:00

Using list manipulation (and showing the steps):

list = {{17, 2}, {11.55, 3}, {18.5, 1}, {14.2, 1}, {14.25, 2}, {11.5,
3}, {15, 2}, {11, 3}, {10.8, 3}};

Times @@@ list

{34, 34.65, 18.5, 14.2, 28.5, 34.5, 30, 33, 32.4}

Total@(Times @@@ list)

259.75

Total@list[[All, 2]]

20

Putting it together in one step:

Total@(Times @@@ list)/Total@list[[All, 2]]

12.9875

Of course, this is why functions are written, i.e., to reduce the work. Note that to find variance and stdev using list manipulation would become difficult and error-prone and the WeightedData approach is more suitable.

WeightedData is both convenient and efficient. For the sake of variety you can also do:

wA = #.Normalize[#2, Total] & @@ Transpose @ # &;
wA @ list
12.9875

and for fun:

☺ = #.#2/(+## & @@ #2) & @@ (#\[Transpose]) &;
☺ @ list
12.9875

Similar to Syed, also the numbers inside the list can be used also #1, #2

Total[{#1*#2} & @@@ list]/Total[list[[All, 2]]]

12.98

• I like your answer!!! using # in functions is very interesting and amazing Sep 14, 2021 at 16:02