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 and 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.
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.