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.
What is median?
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 –
- Sort the values in ascending or descending order (either works).
- If the number of values, n, is odd, then the median is the value in the
(n+1)/2position 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 + 1position in the sorted list(or array) of values.
For example, calculate the median of the following values –
First, let’s sort these numbers 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.
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.
Median of a Python List
To compute the median of a list of values in Python, you can write your own function, or use methods available in libraries like
statistics, etc. Let’s look at these methods with the help of examples.
1. From scratch implementation of median in Python
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))
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
import statistics # create a list ls = [3, 1, 4, 9, 2, 5, 3, 6] # get the median print(statistics.median(ls))
We get the same results as above.
For more on the
statistics library in Python, refer to its documentation.
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))
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.