# Calculating arithmetic mean (one type of average) in Python

###### Posted By: Anonymous

Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers?

## Solution

I am not aware of anything in the standard library. However, you could use something like:

```
def mean(numbers):
return float(sum(numbers)) / max(len(numbers), 1)
>>> mean([1,2,3,4])
2.5
>>> mean([])
0.0
```

In numpy, there’s `numpy.mean()`

.

###### Answered By: Anonymous

### Related Articles

- Most effective way to parse JSON Objects
- problem with client server unix domain stream…
- C++ template,typename and operator
- get an unknown amount of numbers from the user and…
- Fastest way to iterate over all the chars in a String
- Why doesn't the height of a container element…
- How to sum each table of values in each index…
- 3D Rotation of a camera using its own, new axes
- How do I calculate r-squared using Python and Numpy?
- Fastest way to list all primes below N
- How to query for Xml values and attributes from…
- Draw in Canvas by finger, Android
- Can't install via pip because of egg_info error
- Calculating the cumulative sum with a specific…
- Finding the max sum of n consecutive numbers where…
- List of Timezone IDs for use with FindTimeZoneById() in C#?
- Apply multiple functions to multiple groupby columns
- How do I break out of a loop in Scala?
- What makes the different performances between…
- numpy matrix vector multiplication
- data.table vs dplyr: can one do something well the…
- How can I quantify difference between two images?
- How to fix Python Numpy/Pandas installation?
- Error: Expression must have integral or unscoped enum type
- How to compute precision, recall, accuracy and…
- TypeError: ufunc 'add' did not contain a loop with…
- I have just started learning function pointers in C…
- Does moment.js allow me to derive a timezone…
- Generating random whole numbers in JavaScript in a…
- What's the difference between eval, exec, and compile?
- Why does my convolutional model does not learn?
- Finding all possible combinations of numbers to…
- Random normal distribution generator comparison
- Best way to replace multiple characters in a string?
- What do these operators mean (** , ^ , %, //)?
- How to calculate the sum of all columns of a 2D…
- Android Image View Pinch Zooming
- What is the difference between Numpy's array() and…
- Function to calculate R2 (R-squared) in R
- Maximum size of a varchar(max) variable
- collision detection from picturebox in a list c#
- Detect whether a Python string is a number or a letter
- Converting numpy dtypes to native python types
- Most efficient way to generate Histograms of…
- How not to get a repeated attribute of an object?
- Converting between datetime, Timestamp and datetime64
- Code to get sums made of a fibonacci number
- How to convert the following C++ code to C#
- Plotting curve over several subplots in R
- Use awk to find average of a column
- Pandas - Reshape a dataframe columns based on…
- Haskell: ternary tree average, with nested `where`
- Calculate average in java
- Matplotlib plot's title is missing for unknown…
- Better way to shuffle two numpy arrays in unison
- QGraphicsItem don't change pen of parent when chaning child
- Detect if a NumPy array contains at least one…
- Recursive function prints garbage value while…
- munmap_chunk(): invalid pointer Error while making…
- Union of multiple Database queries with same parameters
- python max function using 'key' and lambda expression
- How to generate a random string of a fixed length in Go?
- Pointer to incomplete class type is not allowed
- How to calculate rolling / moving average using…
- Python Plotly Polar Chart Slice Alignment
- ModuleNotFoundError: What does it mean __main__ is…
- Statistical Calculus In Big Data Set Wrong Values
- There's an InputMismatchException, how do i fix it?
- Next.js - Warning: Prop `dangerouslySetInnerHTML`…
- Difference between numpy.array shape (R, 1) and (R,)
- Interrupt an earlier timeout event in Simpy
- Why does the 'int' object is not callable error…
- Expected expression before int
- How to monitor a filtered version of a metric in…
- DAX Previous Year Normalized Amount
- Strange thing with linked list
- Adding up BigDecimals using Streams
- When to use LinkedList over ArrayList in Java?
- Precomputing strided access pattern to array gives…
- How does the @property decorator work in Python?
- How can I print the report without the first in the list
- 'foo' was not declared in this scope c++
- Not able to define Averageifs condition using…
- Combining pyOSC with pyQT5 / Threading?
- How to implement the factory method pattern in C++ correctly
- How do i calculate total without manually inputting it?
- Sorting a multidimensional array using merge sort?
- Draw multiple lines using OpenGL 3.0+ in C
- SQL NVARCHAR and VARCHAR Limits
- Elegant ways to support equivalence ("equality") in…
- How to find 2 parameters with gradient descent…
- How do I limit the number of digits from 6 to 4 in…
- What are type hints in Python 3.5?
- Why is the average of a million random numbers not…
- Assigning a variable NaN in python without numpy
- Is CSS Turing complete?
- python dict to numpy structured array
- What does Ruby have that Python doesn't, and vice versa?
- Collision Problems in Processing
- How can I create an executable to run a kernel in a…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.