Proposal for a built-in average() function

Hello python community,

I would like to propose adding average() as a built-in function to Python, similar to sum(), len(), max(), and min().

Currently, users have to import statistics to use mean(). I believe average() is used frequently enough by beginners and data scientists that it deserves to be a global built-in that requires no import.

MY IMPLEMENTATION :

def average(*args):

"""

Computes the arithmetic mean of the provided arguments.

Supports individual numbers or a single list/tuple.

"""

\# If the first argument is a list or tuple, use that

if len(args) == 1 and hasattr(args\[0\], '\__iter_\_'):

    data = args\[0\]

else:

    data = args

if not data:

    raise ValueError("At least one number is required to compute the average.")



\# Using a list to support generators/iterables

values = list(data)

return sum(values) / len(values)

# Example usage

print(average([10, 20, 30]))

print(average((10, 20, 30)))

print(average(10, 20, 30))

This implementation is flexible because it accepts variable arguments (e.g., average(10, 20, 30)). I would like to hear your thoughts on adding this to the Python documentation and source code.

1 Like

The reason the function is called mean in the stats module is because “average” is not a well-defined term. It can refer to many different calculations. This is a situation where beginners particularly need to learn precision in their terminology.

Besides that–it’s one extra import to bring mean into the namespace if you need it. The tiny convenience of making it built-in doesn’t justify the effort required[1].


  1. even though the effort is relatively small, the benefit is even smaller! ↩︎

9 Likes

Thank you for the feedback! I understand the point about mathematical precision and the ambiguity of the term ‘average.’ My goal was to lower the barrier for absolute beginners who might not yet be familiar with the statistics module. However, I appreciate the community’s focus on clear terminology. I will continue to maintain this as a utility package for those who prefer the simpler naming convention!

Are you aware that you can simply advise new users to use from statistics import mean as average? That one line seems likely to be much easier for new users[1] than having to learn how to find your utility module, install it, and import the average function from it.


  1. Plus, it teaches them a little more of the capabilities of the import statement and the stdlib! ↩︎

8 Likes

People often point to “beginners” to justify their design choices, but I think this is usually really weak justification. Beginners use AI nowadays. Try it: “How do you take the average of numbers in Python?”

When it comes to average versus mean, it would be different if one were much easier to read.

1 Like

The same is true of the term “mean” – there’s the arithmetic mean, the
geometric mean, the harmonic mean, and maybe others.

On its own, the word “mean” is usually taken to be the arithmetic mean.
I’d argue that the same is true of the word “average”, i.e. a
non-technical person wanting an “average” is probably thinking of the
arithmetic mean. If they’re not, they’re likely sophisticated enough to
seek out and use the appropriate function from statistics or elsewhere.

3 Likes

That’s true–“mean” is more precise than “average” but still not exact.

Hi Iresh, welcome to the forum :slight_smile:
Means/averges can get complicated. There is the precise naming - that others have mentioned, but as soon as you allow floats you get issues of precision and also what to do with non-numerical values, float("nan"). Their are, for example, other algorithms (plural), for calculating arithmetic means that better handle float values that can be very large and also very small, to give more accuracy. One might not know one needs them until something breaks down the line…