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?
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:

&gt;&gt;&gt; np.hstack(a).mean()
10.79


Or equivalently (but more verbose in my opinion):

&gt;&gt;&gt; np.mean(np.hstack(a))
10.79