Finding the average of a list
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding how to find the average of a list is a fundamental skill in data analysis and statistics. The average, also known as the mean, provides a measure of the central tendency of a data set. This article will guide you through the process of calculating the average in various contexts, enhance your understanding with technical explanations, and showcase examples to solidify the concept.
Definition of Average
The average (or arithmetic mean) of a list of numbers is obtained by summing all the numbers in the list and then dividing the total by the number of elements in the list. The formula can be expressed as:
Where: • The "Sum of all elements" is calculated by adding up all the values in the list. • The "Number of elements" refers to the total count of numbers present in the list.
Calculating the Average: Step-by-Step
- Identify the List: Start with a specific list of numbers. For example, consider the list: 10, 20, 30, 40, 50.
- Sum the Elements: Add up all the elements in the list:
- Count the Elements: Determine the number of elements in the list. In this case, there are 5 numbers.
- Apply the Formula: Use the formula to find the average:
Thus, the average of the list is 30.
Technical Explanation with Examples
Consider a list where the numbers represent the weekly expenses of a household over four weeks: .
Step 1: Summing the Elements
For our list , we calculate the sum:
Step 2: Counting the Elements
The number of elements in the list is 4.
Step 3: Calculating the Average
Substitute the values into the average formula:
Therefore, the average weekly expense is $225.
Special Cases
- Empty List: If a list is empty (i.e., contains no numbers), the average is undefined because you cannot divide by zero.
- Mixed Data Types: In some cases, lists might contain non-numeric data. Only numerical data can be averaged, so it's crucial to filter out non-numeric entries.
- Weighted Average: Sometimes, different elements carry different levels of importance or weight. A weighted average can be calculated with the formula:
where are the values and are the corresponding weights.
Example of Weighted Average Calculation
Consider a student’s grades in subjects with varying weightings:
| Subject | Grade (x_i) | Weight (w_i) |
| Mathematics | 85 | 5 |
| Science | 90 | 3 |
| Literature | 80 | 2 |
- Calculate the weighted sum:
- Sum the weights:
- Compute the weighted average:
The student's weighted average grade is 85.5.
Summary Table
| Type of Average | Calculation Method | Example Result |
| Simple Average | Sum of elements / Number of elements | |
| Weighted Average | $\frac\{\sum (x_i \cdot w_i)\}\{\sum w_i\}$ | $\frac\{855\}\{10\} = 85.5$ |
| Average of Empty List | Undefined | N/A |
| Mixed Data Types | Filter non-numeric before calculation | Numeric Average |
Understanding averages is critical for data interpretation across numerous applications. By applying these principles, data analysts, statisticians, and students can derive meaningful insights from numeric data efficiently and accurately.
Related reading
- Finding the best cosine similarity in a set of vectors
- Finding the correlation matrix
- Finding the kth quantiles of an n-elements set. From cormen
- Finding the Longest Common Substring in a Large Data Set
- Finding the first duplicate in an int array, java
- Finding the first n largest elements in an array
- Finding the index of elements based on a condition using python list comprehension
- Finding the source code for built-in Python functions?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.