Python Median Function

“When it comes to statistical data analysis, Python is a highly well-liked language. The statistics package’s median() method will be utilized to determine the median value of an unordered list.  The fact that the data set will not need to be arranged before being provided as an argument to the median() method is the function’s main benefit. The median is the number that divides a set of data or conditional probabilities into two halves.

The median value has a considerable advantage over the mean because it is less influenced by incredibly larger or smaller figures. Perhaps the median value is present in the given set, or it will not substantially deviate from the dataset. The middle number in an odd dataset is the median element. The average of the two middle items determines the median number for any even collection of items. We will discuss how to get the median value in python.”

Example no 1

Let’s examine how we will use an inbuilt method in Python to determine the median value. There has been a statistics package in Python. This package offers relevant statistical and predictive analysis related to quantitative techniques. The median() method is one of this package’s notable techniques. This parameter defines the median of a particular dataset, as the name would indicate. First, we have to integrate the statistics framework into the program. Here is an instance of how to ascertain the median value of a given sequence of numbers.

import statistics

d_1 = [3, -21, 13, 76, 97, 54, 57, -31]

print("The median can be seen as : % s "

        % (statistics.median(d_1)))

First of all, we are going to import the required header file statistics. This module deals with mathematical functionalities. In the next line, a variable named “d_1” will be declared. Within this variable, we will define 8 values. These values contain both positive and negative numbers. We want to acquire the median of these values. To terminate the code, we have called the print() method to display the median value. To find out the median value, we will utilize the inbuilt median() method of the statistics library.

Example no 2

In Python, we may utilize the median() function for determining the list’s median value. The values in the list, therefore, do not need to be in any specific sequence, and the list could be any length. The method will return the mean of the two middle entries if the collection has even elements in the list. The middle integer of statistical data is returned by the median() method. In this instance, we will see how to utilize the median() method to validate the median value of different data sets.

from statistics import median

from fractions import Fraction as fr

d_1 = (21, 23, 24, 45, 77, 99, 1)

d_2 = (1.4, 4.7, 4.0, 81.8)

d_3 = (fr(11, 21), fr(4, 82),

fr(12, 9), fr(6, 5))

d_4 = (-9, -7, -4, -2, -33)

d_5 = (-2, -5, -9, -5, 1, 6, 8, 1)

print("1st Median is % s" % (median(d_1)))

print("2nd Median is % s" % (median(d_2)))

print("3rd Median is % s" % (median(d_3)))

print("4th Median  is % s" % (median(d_4)))

print("5th Median is % s" % (median(d_5)))

We begin the code by integrating the median package from the statistics header file. Similarly, the fraction module will be integrated as fr. We will define five lists that contain different values. The first list is stored in a variable “d_1”. This list has seven positive numbers. The second list consists of some floating-point numbers. These values are kept in a variable “d_2”. Here we will create a list of fractional values.

To define the fractional numbers. We utilize the fr() method. The fourth list is stored in the variable “d_4”. Here we specify the set of all the negative values. To store the elements of the 5th list, we declare a variable “d_5”. This tuple has some values of positive as well as negative numerals. Now we want to print the median values of all the directly above data sets. So we call the print() method for all these sets respectively. To acquire the median values, we apply the median() method to the data sets.

Example no 3

Now, if we have to create the median method from scratch, this is an effective method. But in terms of saving time, we will utilize an inbuilt method for basic mathematical computations. Users must comprehend how to determine the median if they intend to apply the median expression.

def get_median(l):

    ls_sorted = l.sort()
    if len(l) % 8 != 0:    
        m11 = int((len(l)+1)/25 - 1)
        return l[m11]
        m_1 = int(len(l)/2 - 1)
        m_2 = int(len(l)/2)
        return (l[m_1]+l[m_2])/2

l = [13, 16, 94, 19, 21, 35, 3, 6]


Here we will define the get_median() function to get the data set. Next, we will sort the required data list. This can be done by using the sort() function. We will utilize the if-else statement. Within this statement, first, we find the length of the data set by using the len() method. The length of the data set would be important to find because it shows whether the defined list will be odd or even in length.

To check this, we use the (!=) operator. If the total values of the list are odd, then we will subtract 1 as the index number starts at 0. Further, we will specify the elements of the list; these elements will be stored in a variable “l”. In the end, we call the print() method to show the median value of the required list. Within this function, we pass the function get_median() as a parameter to this function. By using this method, we will acquire the median value of the list.


In this manual, we have talked about various methodologies that are used to compute the median value. The measurement of a dataset’s relevance is its median value. Whenever the mean computation yields inaccurate findings, it is helpful. Employ Python’s inbuilt median() method that is associated with the statistics package to get the median. The method selects the midpoint and reverts it if the list’s length is odd. The method chooses the two middle numbers in an even set, computes the average, and then presents the outcome. In this guide, we have executed some examples in which we utilize the inbuilt method median() to get the median value of the list. And in one of the instances, we have determined the median value of several data sets.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content