Python – Get median of a List

In this tutorial, we will look at how to get the median value of a list of values in Python. We will walk you through the usage of the different methods with the help of examples.

Median of a list in Python.

Median is a descriptive statistic that is used as a measure of central tendency of a distribution. It is equal to the middle value of the distribution. There are equal number of values smaller larger than the median. It is also not much sensitive to the presence of outliers in the data like the mean (another measure of central tendency).

To calculate the median of a list of values –

  1. Sort the values in ascending or descending order (either works).
  2. If the number of values, n, is odd, then the median is the value in the (n+1)/2 position in the sorted list(or array) of values.
    If the number of values, n, is even, then the median is the average of the values in n/2 and n/2 + 1 position in the sorted list(or array) of values.

For example, calculate the median of the following values –

A bunch of numbers whose median is to be calculated.

First, let’s sort these numbers in ascending order.

numbers sorted in ascending order

Now, since the total number of values is even (8), the median is the average of the 4th and the 5th value.

Median calculation

Thus, median comes out to be 3.5

Now that we have seen how is the median mathematically calculated, let’s look at how to compute the median in Python.

To compute the median of a list of values in Python, you can write your own function, or use methods available in libraries like numpy, statistics, etc. Let’s look at these methods with the help of examples.

You can write your own function in Python to compute the median of a list.

def get_median(ls):
    # sort the list
    ls_sorted = ls.sort()
    # find the median
    if len(ls) % 2 != 0:
        # total number of values are odd
        # subtract 1 since indexing starts at 0
        m = int((len(ls)+1)/2 - 1)
        return ls[m]
    else:
        m1 = int(len(ls)/2 - 1)
        m2 = int(len(ls)/2)
        return (ls[m1]+ls[m2])/2

# create a list
ls = [3, 1, 4, 9, 2, 5, 3, 6]
# get the median
print(get_median(ls))

Output:

3.5

Here, we use the list sort() function to sort the list, and then depending upon the length of the list return the median. We get 3.5 as the median, the same we manually calculated above.

Note that, compared to the above function, the libraries you’ll see next are better optimized to compute the median of a list of values.

You can also use the statistics standard library in Python to get the median of a list. Pass the list as argument to the statistics.median() function.

import statistics

# create a list
ls = [3, 1, 4, 9, 2, 5, 3, 6]
# get the median
print(statistics.median(ls))

Output:

3.5

We get the same results as above.

For more on the statistics library in Python, refer to its documentation.

The numpy library’s median() function is generally used to calculate the median of a numpy array. You can also use this function on a Python list.

import numpy as np

# create a list
ls = [3, 1, 4, 9, 2, 5, 3, 6]
print(np.median(ls))

Output:

3.5

You can see that we get the same result.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *