6 Ways to Get the Median of a Python List

Rate this post

Problem Formulation

Given a Python list of integer or float numbers.

How to calculate the median of a Python list?

Formally, the median is “the value separating the higher half from the lower half of a data sample” (wiki).

Note that the median is different to the mean or average as can be seen in the following graphic:

If there are an even number of elements in the list (i.e., len(list)%2==0), there is no middle element. In this case, the median can be the average of the two middle elements.

Method 1: statistics.median()

The most straightforward way to get the median of a Python list your_list is to import the statistics library and call statistics.median(your_list). The statistics library is included in the Python standard libraries, so it doesn’t have to be manually installed.

Here’s a simple example:

import statistics


def get_median(lst):
    return statistics.median(lst)


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 2.5

We create two lists:

  • 3 is the median of the list [3, 2, 4, 7, 1] as can be seen in the sorted representation [1, 2, 3, 4, 7].
  • 2.5 is the median of the list [3, 2, 4, 7, 1, 1] as can be seen in the sorted representation [1, 1, 2, 3, 4, 7] and (2+3)/2 is 2.5.

Method 2: No Library Approach

To get the median of a Python list without library support, perform the following three steps:

  • Sort the list.
  • Get the index of the left mid element.
  • Average the left and right mid elements.

This is done in the three Python lines:

  • tmp = sorted(lst)
  • mid = len(tmp) // 2
  • res = (tmp[mid] + tmp[-mid-1]) / 2

The third line contains the median of the Python list. This works for lists both with an even and an odd number of elements.

We use negative list indexing to access the right mid element. If the list has an odd number of elements, the left and right mid indices are actually the same in which case the value of the single mid element is returned.

Here’s an example:

def get_median(lst):
    tmp = sorted(lst)
    mid = len(tmp) // 2
    return (tmp[mid] + tmp[-mid-1]) / 2


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 2.5

It should be noted that the naive approach of not averaging the two mid elements in the case of a list with an even number of elements is often sufficient too:

Method 3: Naive No-Library Approach

If you’re okay with returning the first mid element when searching the median of a list with an even number of elements, you can use the following approach:

  • Sort the list.
  • Get the index of the left mid element (in case the list length is even) and the index of the single mid element (in case the length of the list is odd).
  • Return the median by accessing the mid element in the sorted list.

In particular, the three lines in Python do the job:

  • tmp = sorted(lst)
  • mid = len(tmp) // 2
  • res = tmp[mid]

The variable res contains the median of the list.

Here’s an example:

def get_median(lst):
    tmp = sorted(lst)
    mid = len(tmp) // 2
    return tmp[mid]


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 3

Please note that this is not necessarily the statistical sound way of calculating the median for a list with an even number of elements.

Method 4: np.median()

You can get the median of a Python list your_list by importing the numpy library and call numpy.median(your_list).

Here’s a simple example of how we use NumPy to calculate the median of a Python list:

import numpy as np


def get_median(lst):
    return np.median(lst)


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3.0


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 2.5

We create two lists:

  • 3 is the median of the list [3, 2, 4, 7, 1] as can be seen in the sorted representation [1, 2, 3, 4, 7]. NumPy converts all outputs to float if possible.
  • 2.5 is the median of the list [3, 2, 4, 7, 1, 1] as can be seen in the sorted representation [1, 1, 2, 3, 4, 7] and (2+3)/2 is 2.5.

What’s the difference between numpy.median() and statistics.median()

Unlike the statistics library, the numpy library is not included in the Python standard libraries, so it must be manually installed if you haven’t already.

That’s why I recommend using statistics.median() rather than numpy.median() if all you want to do is calculating the median of a Python list.

Also, statistics.median() returns an integer value for integer lists with an odd number of elements whereas numpy.median() always returns a float. Otherwise, both functions are the same.

Related Tutorial: How to Install NumPy in Python?

Method 5: np.percentile()

A generalized approach to calculating the median of a list my_list of numbers is to use the np.percentile(my_list, 50) function that returns the exact 50th percentile of the list. The 50th percentile is the median.

Definition: 50th Percentile – Also known as the Median. The median cuts the data set in half. Half of the answers lie below the median and half lie above the median. (source)

Here’s the code example:

import numpy as np


def get_median(lst):
    return np.percentile(lst, 50)


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3.0


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 2.5

Method 6: Basic Python in Multiple Lines

A simple approach to finding the median of a Python list is to handle evenly-sized and oddly-sized lists differently after sorting the list:

  • If the list has an odd number of elements, return the median right away by using len(l)//2 to get the index of the mid element.
  • Otherwise, average the two elements in the middle of the sorted list.

Here’s the code snippet that implements this approach — comments for explanation of the relevant parts:

def get_median(lst):
    l = sorted(lst)
    mid = len(l) // 2
    if len(lst)%2:
        # list is odd-sized:
        # single median exists
        return l[mid]
    else:
        # list is evenly-sized:
        # average two mid values
        return (l[mid-1]+l[mid])/2


odd = [3, 2, 4, 7, 1]
print(get_median(odd))
# 3.0


even = [3, 2, 4, 7, 1, 1]
print(get_median(even))
# 2.5

Related Video – Finding the Median of a Python List

How to Find the Median of a List in Python TUTORIAL (Common Python Interview Question)

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!