How to find median of a list in Python

In this article, you will see how we can find the median of a list using python programming. We will use different methods to find the median with Python. But before deep diving let me tell you what the median actually is?

In simple words, median is the middle value of an ascending or descending list.

Why do we need to find median?

Median is just a way to know the insights of the dataset. If we find the mean of the list and that list contains some outliers, then finding mean will not give us more insights into the data set, but the median will.

How to find the median of a list?

  1.  sort the list in ascending or descending order
  2. The middle value of this sorted list is the median of the list
  3. sum the two numbers in the middle
  4. the division of the sum by 2 will give you the median of the list

finding the median of a list having odd numbers

Let’s say we have a list of 5 integral values and we want to find the medina value.

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

  1.  sort the list in either ascending or descending order.
  2. Let’s say we order the list in descending order. the list will be [1, 2, 3, 4, 5]
  3.  the middle value is the median of this list. In this case, ‘3’ is the middle value.
  4. So we will say that the median of this list is 3.

Fining the median of a list having even numbers

To find the median of a list having even numbers is simple. Let’s say we have a list of 4 items and we want to find the median of that list.

list = [ 4,5,1,9]

  1.  first sort the list in either ascending or descending order.
  2. Let’s say we sort the above list in descending order this time. the list will be [ 9, 5, 4, 1]
  3.  the list contains an even number of elements.
  4. so we will find the sum of the middle numbers. That is the sum of the 5 and 4
  5.  the sum of 4 and 5 is 9. Now we will divide the sum by 2.
  6. In this case, we need to divide 9 by 2. 9/2 is 4.5.
  7.  the median of the list is 4.5

How do you find the median of a list in Python?

Finding median in Python is just applying the same operation that we did before manually. To find the median of a list using python follow the following steps:

  1.  sort the list using the sort(list) method function
  2.  find middle index by dividing the length of the list by 2
  3.  find the floor value of the mid index so that it should not be in an integer value.
  4.  find the value of the list lies on that middle index. and that is the median of the list

Python code to find the median of a list of odd numbers

import math
list = [ 1 ,9, 4,5,3]
list.sort()
middle_index= len(list)/2
exact_middle_index = math.floor(middle_index)
median_value = list[exact_middle_index]
print(median_value) 

Python Code to find the median of a list of even numbers

To find the medina of a list of even numbers, follow the following steps.

  • sort the list
  • find the two middle indices
  • sum the values lies in the middle indices
  • divide the sum by 2 and this is the median of the list

Check the following code to find the median of a list having even numbers of elements.

import math
list = [2,3,1,6]
list.sort()
first_ind = math.floor( len(list)/2)
print(first_ind)
second_ind =math.floor( (len(list)/2)-1)
print(second_ind)
sum_of_middle_values = list[first_ind]+list[second_ind]
median= sum_of_middle_values/2
print(median)

Use Python statistics module to find the median of a list in Python

Python provides the statistics module. Which contain a lot of utility functions to do statistics calculations. You can find the mean value of a list in python using the statistics module. The statistics module has statistics.median() a a function which returns the median value of a list. It is type-safe as well.

Python Statistics module to find median of a list

import statistics as st
list = [3,4,2,1]
median_value = st.median(list)
print(median_value)

Use Python Numpy module to find the median value of a list

Finding the median value of a list is so frequent and common that every python module having scientific calculations capability will have this utility function. The Python numpy module have also a function, that will find the median value of a list or an array-like object.
Python Numpy to find the median value of a list

import numpy as np
list = [3,4,2,1]
median_value = np.median(list)
print(median_value)

Create a Python Function that finds the Median of a list

You can create a python function that can help you find the median of the list. Following is a python function that you can use to find the median value of any list.

def median(lst):
    sortedLst = sorted(lst)
    lstLen = len(lst)
    index = (lstLen - 1) // 2
   
    if (lstLen % 2):
        return sortedLst[index]
    else:
        return (sortedLst[index] + sortedLst[index + 1])/2.0
    
median([4,42,5,1])

Find the median of an array in python

An array can be considered as a list unless you use the numpy array for that. The following python code will find the median value of an array using python.

def median(array):
    array = sorted(array)
    half, odd = divmod(len(array), 2)
    if odd:
        return array[half]
    return (array[half - 1] + array[half]) / 2.0

What are the low median and high median of a list?

When the data points are in a list or odd then the median is the middle number of the sorted list. The problem occurs when we have even numbers of elements in a list. Here comes the problem, In general, we find the sum of the two middle numbers of that sorted list and return the division of that sum by 2.

But we might sometimes want to know the lower and higher of these two values. Here comes the concept of low median and high median values of a list.

low median of a list in python

The low median value of a list is the lesser value of the 2 middle points of a list with an even number. For example, we have a list of even numbers [ 1, 3,2, 5]. To calculate the low median use the following steps.

  • sort the list
  • find the middle points
  • compare the values of the middle points
  • return the lesser value of these middle values
  • this is the low median value of the list

With the Python statistics module you can use the statistics.median_low() function to get the low meidan of a list.

import statistics as st
list =[4,3,2,7]
low_m = st.median_low(list)
print(low_m)

# the low median value be 3

High median of a list in Python

By the high median we mean the higher of the two middle values of a sorted list of even numbers of elements.To find the high median of a list using the following steps.

  • sort the list
  • find the two middle points
  • return the high values between those two middle points
  • this value be the high median of that list

Use the following code to find the high median of a list in Python

import statistics as st
list =[4,3,2,7]
high_m = st.median_high(list)
print(high_m)

# the high median value be 4

Leave a Comment

Scroll to Top