Skip to main content

How to Calculate Mean, Median, and Mode in Python Without Libraries

Mean, median, and mode are the three most fundamental measures of central tendency in statistics. While Python offers built-in libraries like statistics to compute these values effortlessly, understanding how to calculate them from scratch deepens your grasp of the underlying algorithms and helps in scenarios where external libraries aren't available.

This guide walks through each calculation step by step, with clear examples and explanations.

Calculating the Mean (Average)

The mean (or arithmetic average) is the sum of all values divided by the number of values. It gives you a sense of the "center" of your data.

Formula: mean = sum of elements / number of elements

numbers = [1, 2, 3, 4, 5]

mean = sum(numbers) / len(numbers)

print("Mean:", mean)

Output:

Mean: 3.0

The built-in sum() function adds all elements, and len() returns the count. Dividing one by the other gives the mean.

  • Time Complexity: O(n)
  • Space Complexity: O(1)
tip

For very large datasets or when numerical precision matters (e.g., summing many floats), consider using math.fsum() instead of sum() to reduce floating-point rounding errors:

import math

numbers = [0.1, 0.2, 0.3, 0.4, 0.5]
mean = math.fsum(numbers) / len(numbers)
print("Mean:", mean)

Calculating the Median (Middle Value)

The median is the middle value when the data is sorted in ascending order. If the dataset has an even number of elements, the median is the average of the two middle values.

numbers = [1, 2, 3, 4, 5]
numbers.sort()
n = len(numbers)

if n % 2 == 0:
median = (numbers[n // 2 - 1] + numbers[n // 2]) / 2
else:
median = numbers[n // 2]

print("Median:", median)

Output:

Median: 3

How It Works

  1. Sort the list in ascending order.
  2. Check if the number of elements is even or odd.
  3. If odd, the median is the element at the middle index (n // 2).
  4. If even, the median is the average of the two elements at indices n // 2 - 1 and n // 2.

Let's verify with an even-length list:

numbers = [1, 2, 3, 4, 5, 6]
numbers.sort()
n = len(numbers)

if n % 2 == 0:
median = (numbers[n // 2 - 1] + numbers[n // 2]) / 2
else:
median = numbers[n // 2]

print("Median:", median)

Output:

Median: 3.5
  • Time Complexity: O(n log n) because dominated by the sorting step.
  • Space Complexity: O(1) because sort() operates in place.
Don't forget to sort first

A common mistake is calculating the median on an unsorted list, which gives an incorrect result:

numbers = [5, 1, 4, 2, 3]
n = len(numbers)

# Wrong: not sorted!
wrong_median = numbers[n // 2]
print("Wrong median:", wrong_median) # Output: 4 (incorrect)

# Correct: sort first
numbers.sort()
correct_median = numbers[n // 2]
print("Correct median:", correct_median) # Output: 3

Always sort the data before finding the median.

Calculating the Mode (Most Frequent Value)

The mode is the value that appears most frequently in the dataset. A dataset can have one mode, multiple modes (multimodal), or no mode (when all values occur equally).

Using a Dictionary to Count Frequencies

This approach manually builds a frequency dictionary and finds the element(s) with the highest count:

def find_mode(numbers):
frequency = {}
for num in numbers:
frequency[num] = frequency.get(num, 0) + 1

max_count = max(frequency.values())

modes = [k for k, v in frequency.items() if v == max_count]

if len(modes) == len(frequency):
return "No mode found (all values occur equally)"
return modes

numbers = [1, 2, 3, 4, 5, 5]
print("Mode:", find_mode(numbers))

Output:

Mode: [5]

Handling Multiple Modes

When more than one value shares the highest frequency, the function returns all of them:

def find_mode(numbers):
frequency = {}
for num in numbers:
frequency[num] = frequency.get(num, 0) + 1

max_count = max(frequency.values())

modes = [k for k, v in frequency.items() if v == max_count]

if len(modes) == len(frequency):
return "No mode found (all values occur equally)"
return modes

numbers = [11, 8, 8, 3, 4, 4, 5, 6, 6, 6, 7, 8]
print("Modes:", find_mode(numbers))

Output:

Modes: [8, 6]

Handling No Mode

When every element occurs the same number of times, there is no meaningful mode:

def find_mode(numbers):
frequency = {}
for num in numbers:
frequency[num] = frequency.get(num, 0) + 1

max_count = max(frequency.values())

modes = [k for k, v in frequency.items() if v == max_count]

if len(modes) == len(frequency):
return "No mode found (all values occur equally)"
return modes

numbers = [1, 2, 3, 4, 5]
print(find_mode(numbers))

Output:

No mode found (all values occur equally)
  • Time Complexity: O(n)
  • Space Complexity: O(n) because of the frequency dictionary.

Using Counter from collections

While we're avoiding heavy external libraries, collections.Counter is part of Python's standard library and provides a cleaner way to count element frequencies:

from collections import Counter

def find_mode(numbers):
counts = Counter(numbers)
max_count = max(counts.values())

modes = [k for k, v in counts.items() if v == max_count]

if len(modes) == len(counts):
return "No mode found (all values occur equally)"
return modes

numbers = [1, 2, 3, 4, 5, 5, 5, 6, 6]
print("Mode:", find_mode(numbers))

Output:

Mode: [5]
note

Counter is part of Python's standard library (collections module), not a third-party package. It's available in both Python 2 and 3 without any installation.

Putting It All Together

Here's a complete, reusable implementation that calculates all three measures from a single list:

from collections import Counter

def calculate_mean(numbers):
return sum(numbers) / len(numbers)

def calculate_median(numbers):
sorted_nums = sorted(numbers)
n = len(sorted_nums)
if n % 2 == 0:
return (sorted_nums[n // 2 - 1] + sorted_nums[n // 2]) / 2
return sorted_nums[n // 2]

def calculate_mode(numbers):
counts = Counter(numbers)
max_count = max(counts.values())
modes = [k for k, v in counts.items() if v == max_count]
if len(modes) == len(counts):
return "No mode"
return modes

# Example dataset
data = [4, 1, 2, 2, 3, 5, 4, 2, 7, 8]

print("Mean:", calculate_mean(data))
print("Median:", calculate_median(data))
print("Mode:", calculate_mode(data))

Output:

Mean: 3.8
Median: 3.5
Mode: [2]

Quick Reference

MeasureWhat It RepresentsTime ComplexityKey Consideration
MeanAverage of all valuesO(n)Sensitive to outliers
MedianMiddle value when sortedO(n log n)Requires sorted data
ModeMost frequent value(s)O(n)May return multiple values or none

Conclusion

Implementing mean, median, and mode from scratch is an excellent exercise for understanding how these statistical measures work at the algorithmic level.

For production code and real-world projects, Python's built-in statistics module provides mean(), median(), and mode() functions that handle edge cases and numerical precision out of the box.

However, knowing how to build these calculations yourself ensures you can work in any environment and gives you the flexibility to customize the logic as needed.