Search code examples
pythonlistaverage

averaging every nth element of multiple lists


I have 4 lists of the same length and would like to average every nth element across the lists and make a new list out of these averaged values. As an example:

y1 = [1, 2, 2, 4, 5]
y2 = [3, 6, 9, 12, 0]
y3 = [2, 3, 4, 5, 6]
y4 = [2, 1, 5, 7, 9]

The expected result should be:

y1234  = [2, 3, 5, 7, 5]

How can I do this?


Solution

  • You can use numpy.mean

    np.array([y1, y2, y3, y4]).mean(axis=0)