# Mean of combined sublists with different lengths

I have a seemingly simple issue. Consider this list:

a = [[12.0, 5.0, 63.0], [0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4]]


I want to find the mean (using numpy) of all the elements in its sublists combined. In this case the result should be:

10.79


obtained as:

np.mean([12.0, 5.0, 63.0, 0.1, 2.0, 7.1, 3.0, 2.3, 5.0, 8.4])


The solution I've found is to flatten the list first and then obtain the mean, as:

np.mean([item for sublist in a for item in sublist])


but this seems unnecessarily complicated. I would've assumed that numpy.mean() could handle this case without the need to modify the list first. I tried using the argument axis to no avail.

Am I missing some functionality here?

• How is that complicated? It's literally doing exactly what you specified in your problem statement, nothing more. It's also literally the simplest list comprehension you can write over a nested list. Jul 12 '16 at 18:50
• "Complicated" in the sense that I expected numpy to automatically handle such a case via some argument. Not "complicated" as in "the code is hard to read/comprehend". Jul 12 '16 at 18:51
• If you use the numpy array data structures I'm pretty sure it automatically flattens the data for you without needing to pass an argument. Jul 12 '16 at 18:54
• So just cast to np.array Jul 12 '16 at 18:56
• @machineyearning: that won't work because the lists are different sizes. OP needs to flatten first. Jul 12 '16 at 19:38

Assuming your nesting is only one level deep, the concatenation can be done very easily using np.hstack. This will treat the inner lists as vectors, then concatenate them end-to-end as a 1D numpy array. You can then take the mean of the resulting array. So this will do what you want:

>>> np.hstack(a).mean()
10.79


Or equivalently (but more verbose in my opinion):

>>> np.mean(np.hstack(a))
10.79

• Excellent, I hadn't though of using np.hstack(). Thank you! Jul 12 '16 at 19:49
• But look at what hstack does: np.concatenate([atleast_1d(_m) for _m in tup],axis=0). It does a list comprehension on your list. Actually that isn't needed here. This will do: np.mean(np.concatenate(a)). The key is understanding what this does compared to np.array(a). Most cases when you apply a numpy function to a list, it first turns that list into an array (or maybe a list of arrays). Jul 17 '16 at 18:07