Mean method in math module

I noticed that python’s math module does not present a method that averages a list.

I agree that the function that calculates the mean is easy to write but it would be interesting to add it in the python math module for completeness and for the fact that an implementation in the math package would be optimized.

In [3]: import statistics

In [4]: statistics.mean([1,2,3,4,5,6])
Out[4]: 3.5
3 Likes

The math module is for mathematical functions and constants - specifically, it mirrors a few that are provided by the C standard, as the documentation describes.

It is not for “every kind of calculation a mathematician might want to do” - in particular, it’s not for things that are a matter of arithmetic.

However, computing a mean is provided by the statistics standard library module. That one is for the purpose of common calculations that statisticians (i.e., a very specific group of mathematicians) need to do for their work. In statistics, computing the mean of a population (or a sample) is an important, everyday task. For someone who is doing trigonometry or calculus, on the other hand, it might have almost no relevance. The distinction is somewhat arbitrary (for example, math.comb implements the “N choose k” function in combinatorics, which is needed a lot of the time to compute probabilities, which statisticians may care about), but there is some amount of reasoning behind it.

More generally: if something seems to be missing, do a broader search for it first. For example, using a search engine.

Actually, this is not true: the mean is used in a lot of areas of mathematics (or almost all areas). For example, harmonic analysis, geometric group theory, amenability theory, and probability (which is not statistics). You could ask a random mathematician in which area of mathematics the mean function belongs, and probably he will not answer just statisticians. The point here is that statisticians are the more extensive group of mathematicians who use coding for their jobs. Therefore, the method mean is in the module statistics from a coding point of view because statistician mainly uses it.

But my question was legit because for a (pure) mathematician, the mean should be in the math package as it is a function used everywhere (more than math.gcd of math.comb), and forums are made to ask questions.

Yes, it’s a legit question. I hope that “it can be found in the statistics module” is an acceptable answer :slight_smile: Sometimes, you’ll find the exact thing you want, but not in the place where you first looked; that doesn’t mean you’re dumb or a bad programmer, but just that there are often multiple plausible places to put something. For example, Python has a filter function built in, but filterfalse (the converse of filter) is found in the itertools module. Nothing’s perfect, and we have modules that try to be as consistent as possible. Hopefully import statistics can solve your problem!