Median is a descriptive statistic. It is often used as a measure of the central tendency of a distribution. In this tutorial, we will look at how to get the median of values in a tuple in Python with the help of examples.
How to calculate the median?
Median is defined as the middle value of a distribution. That is, the number of values smaller than the median is the same as the number of values larger than the median.
For example, for the numbers 1, 2, 3, 4, 5 the median value is 3 as it lies in the middle of the distribution.
From this we can see that to calculate the median, first, sort the values and then pick the middle value. If the total number of values (N) is even, then the median is the average of the (N/2)th and the (N/2 + 1)th value.
Now that we know how to compute the median mathematically, let’s see how to do it in Python.
Median of a Tuple in Python
To get the median of a tuple in Python, you can write your own custom function or use functions defined in libraries such as statistics
, numpy
, etc.
Median of a tuple from scratch in Python
Let’s write a function to compute the median.
def get_median(t): # sort the tuple and store the resulting list ls = sorted(t) # 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 tuple t = (5, 2, 1, 3, 4) # get the median print(get_median(t))
Output:
Introductory ⭐
- 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
Intermediate ⭐⭐⭐
- 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
Advanced ⭐⭐⭐⭐⭐
- 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.
3
Here, we use the sorted()
function to get a sorted copy of the tuple elements as a list. We then proceed to compute the median from this sorted list.
You can also use off-the-shelf libraries in Python to directly compute the median. These libraries have better-optimized implementations.
Using statistics
library
You can use the median()
function from the statistics
standard library in Python to get the median of an iterable. Let’s look at an example.
import statistics # create a tuple t = (5, 2, 1, 3, 4) # get the median print(statistics.median(t))
Output:
3
We get the same result as above.
Using numpy
library
You can also use the numpy
library’s median()
function to compute the median of a tuple. Here’s an example.
import numpy as np # create a tuple t = (5, 2, 1, 3, 4) # get the median print(np.median(t))
Output:
3.0
We get the same result we got in the above examples.
You might also be interested in –
- Python – Get median of a List
- Sort a Tuple in Python – With Examples
- Get Average of Elements in a Python Tuple
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.