How to calculate median in python?

The Median is the middle value of a sequence sorted in ascending or descending order. If the sequence has an odd number of elements, the median is the middle term of the sequence. Consider a list, list_1 = [1, 2, 3]. The number of elements in the list is odd. Hence median is the middle element that is 2.

For the sequence having an even number of elements, the median is calculated as the mean of two middle elements. Consider a list, list_1 = [1, 2, 3, 4]. The number of elements in the list is even. Hence median = (2+3)/2. The median is 2.5.

There are several ways to calculate the median by using the statistics module, or by using the sort() function and arithmetic operators.

Calculating the median using statistics module

Python supports a built-in module known as statistics, to calculate statistics of numeric data. Statistics module is embedded with various functions such as mean(), median(), mode(), etc.

The main advantage of using the built-in median() function is that the sequence does not need to be sorted, before passing the sequence as a parameter to the median() function. The syntax for the median() function is given below.

#syntax:
statistics.median(sequence)

For example, Consider a list num_list = [5, 3, 2, 8, 9, 1]. The statistics.median(num_list) returns median value as 4.0.

import statistics
num_list = [5, 3, 2, 8, 9, 1]
print("num_list: ", num_list)
median_1 = statistics.median(num_list)
print("The median of all the terms is: ", median_1)

Output

num_list:  [5, 3, 2, 8, 9, 1]
The median of all the terms is:  4.0

Calculating the median using sort() and arithmetic operation

To calculate the median for a sequence of items the list must be in sorted order. Let us consider a list num_list = [5, 3, 2, 8, 9, 1] . The list is sorted in ascending order using the sort() function. num_list. sort() modifies the num_list to [1, 2, 3, 5, 8, 9]. The len() function calculates the number of elements in the list and stores the value in the variable len_list.

len_list//2 gives the middle index of the list. The number of elements in num_list is even. Hence if block is executed and the result is printed.

num_list = [5, 3, 2, 8, 9, 1]
print("num_list: ", num_list)
num_list.sort()
print("Sorted num_list: ", num_list)
len_list = len(num_list)
index = len_list//2
if len_list % 2 ==0:
    median_1 = (num_list[index-1] + num_list[index])/2
else:
    median_1 = num_list[index]
print("The median of all the terms is: ", median_1)

Output

num_list:  [5, 3, 2, 8, 9, 1]
Sorted num_list:  [1, 2, 3, 5, 8, 9]
The median of all the terms is:  4.0
If anything other than numeric values is present in the sequence a TypeError Exception is raised.

For example, The presence of a string value "Python" leads to a TypeError Exception.

num_list = [5, 3, 2, 8, 9, 1, "Python"]
print("num_list: ", num_list)
median_1 = statistics.median(num_list)
print("The median of all the terms is: ", median_1)

Output

data = sorted(data)
TypeError: '<' not supported between instances of 'str' and 'int'> 
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions