Skip to content

How to Calculate the Median of a List in Python

How to find the median of a Python list using statistics.median, numpy.median, or a simple custom method, with examples and tips for handling lists with odd or even numbers of values.

Calculating the median (the middle value of a dataset) is essential for data analysis, especially when outliers can skew the mean. Python offers multiple ways to calculate this: using the built-in statistics module, the high-performance numpy library, or a custom implementation for educational purposes.

This guide explains all three methods, their performance trade-offs, and how to handle even vs. odd list lengths.

Method 1: Using statistics.median (Standard Library)

For standard Python lists, the built-in statistics module is the most Pythonic and readable choice. It handles integers, floats, and mixed data types automatically.

import statistics

data_odd = [1, 3, 5, 7, 9]
median_odd = statistics.median(data_odd)
print(f"Median (Odd): {median_odd}")

data_even = [1, 3, 4, 6, 8, 10]
median_even = statistics.median(data_even)
print(f"Median (Even): {median_even}")

Output:

Median (Odd): 5
Median (Even): 5.0

Method 2: Using numpy.median (Performance)

If you are working with large datasets or numerical arrays, numpy is significantly faster. It is the standard for scientific computing in Python.

import numpy as np

data = [10, 20, 30, 100, 200]

median_val = np.median(data)

print(f"NumPy Median: {median_val}")

Output:

NumPy Median: 30.0

Method 3: Custom Implementation (No Imports)

Understanding how to calculate the median manually is a great exercise. The logic depends on whether the list length (n) is odd or even.

  1. Sort the list.
  2. Find the middle index.
  3. If Odd: Return the element at the middle index.
  4. If Even: Return the average of the two middle elements.
def calculate_median(data):
    sorted_data = sorted(data)
    n = len(sorted_data)
    
    mid_index = n // 2
    
    if n % 2 != 0:
        return sorted_data[mid_index]
    else:
        lower = sorted_data[mid_index - 1]
        upper = sorted_data[mid_index]
        return (lower + upper) / 2

print(f"Custom Median (Odd): {calculate_median([3, 1, 2])}")
print(f"Custom Median (Even): {calculate_median([4, 1, 3, 2])}")

Output:

Custom Median (Odd): 2
Custom Median (Even): 2.5

Comparing Performance and Use Cases

Method Best Use Case Performance Pros
statistics.median Standard Scripts Moderate Built-in, readable, handles mixed types
numpy.median Data Science / Large Data Very High Vectorized, extremely fast for arrays
Custom Function Learning / Restricted Environments Low No imports required, full control

Conclusion

To calculate the median in Python:

  1. Use statistics.median(data) for general-purpose scripts (simplest).
  2. Use numpy.median(data) for data analysis and large datasets (fastest).
  3. Remember to Sort: If writing a custom function, sorting the data first is mandatory.
Navigation