Average elements in sublists
list, numpy, python
Solution
are you interested in using numpy?
In [19]: list_1 = [[1,3,5], [7,4,9], [3,6,2], [5,4,7]]
In [22]: np.mean(list_1, 0)
Out[22]: array([ 4. , 4.25, 5.75])
Problem
I have a list, created by an iterative process, composed of a variable number of sublists all of them with the same number of elements; which is also variable. For example, on one iteration I can have 4 sublists of 3 elements each, like so: ``` list_1 = [[1,3,5], [7,4,9], [3,6,2], [5,4,7]] ``` and in the next iteration of the code I can have: ``` list_2 = [[2,4,8,3,5], [2,4,9,1,3], [1,9,6,3,6]] ``` that is, 3 sublists of 5 elements each. For a given iteration all sublist will always have the same number of elements. I need a way to generate for iteration `i` a new list out of `list_i`, containing the average of all elements located in the same position in each sublist. So in the first case for `list_1` I'd get: ``` avrg_list = [4.0, 4.25, 5.75] ``` and in the second case for `list_2`: ``` avrg_list = [1.67, 5.67, 7.67, 2.33, 4.67] ``` How can I do this with a flexible code that will adjust itself to different number of sublists and elements?