Python Program to Find Median of a List
To find the median of a list in Python, sort the list and use
median = sorted_list[len(sorted_list)//2] for odd length or average the two middle values for even length.Examples
Input[3, 1, 2]
Output2
Input[4, 1, 7, 2]
Output3.0
Input[5]
Output5
How to Think About It
To find the median, first arrange the numbers in order from smallest to largest. If the list has an odd number of items, the median is the middle number. If it has an even number, the median is the average of the two middle numbers.
Algorithm
1
Sort the list in ascending order.2
Check if the number of elements is odd or even.3
If odd, return the middle element.4
If even, return the average of the two middle elements.Code
python
def find_median(numbers): sorted_list = sorted(numbers) n = len(sorted_list) mid = n // 2 if n % 2 == 1: return sorted_list[mid] else: return (sorted_list[mid - 1] + sorted_list[mid]) / 2 # Example usage print(find_median([3, 1, 2])) # Output: 2 print(find_median([4, 1, 7, 2])) # Output: 3.0
Output
2
3.0
Dry Run
Let's trace the list [4, 1, 7, 2] through the code
1
Sort the list
sorted_list = [1, 2, 4, 7]
2
Calculate length and middle index
n = 4, mid = 2
3
Check if length is odd or even
4 is even, so calculate average of sorted_list[1] and sorted_list[2]
4
Calculate median
median = (2 + 4) / 2 = 3.0
| Step | Sorted List | n | mid | Median Calculation |
|---|---|---|---|---|
| 1 | [1, 2, 4, 7] | 4 | 2 | N/A |
| 2 | [1, 2, 4, 7] | 4 | 2 | N/A |
| 3 | [1, 2, 4, 7] | 4 | 2 | Average of 2 and 4 |
| 4 | [1, 2, 4, 7] | 4 | 2 | 3.0 |
Why This Works
Step 1: Sorting the list
Sorting arranges the numbers so the middle value(s) can be found easily.
Step 2: Finding the middle index
Dividing the length by 2 gives the middle position in the sorted list.
Step 3: Odd or even length check
If the list length is odd, the middle element is the median; if even, the median is the average of the two middle elements.
Alternative Approaches
Using statistics module
python
import statistics def find_median(numbers): return statistics.median(numbers) print(find_median([3, 1, 2])) # Output: 2 print(find_median([4, 1, 7, 2])) # Output: 3.0
This method is simpler and uses built-in functions but requires importing a module.
Manual sorting with bubble sort
python
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr def find_median(numbers): sorted_list = bubble_sort(numbers.copy()) n = len(sorted_list) mid = n // 2 if n % 2 == 1: return sorted_list[mid] else: return (sorted_list[mid - 1] + sorted_list[mid]) / 2 print(find_median([3, 1, 2])) # Output: 2
This shows how to sort manually but is less efficient and more complex.
Complexity: O(n log n) time, O(n) space
Time Complexity
Sorting the list takes O(n log n) time, which dominates the median calculation.
Space Complexity
Sorting creates a new list, so space complexity is O(n). In-place sorting can reduce this.
Which Approach is Fastest?
Using the built-in statistics.median is fastest and simplest for most cases.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Manual sort + median | O(n log n) | O(n) | Learning sorting and median logic |
| statistics.median | O(n log n) | O(n) | Quick and reliable median calculation |
| Manual bubble sort + median | O(n^2) | O(n) | Understanding sorting basics, not efficient |
Always sort the list first before finding the median to ensure correct results.
Forgetting to sort the list before finding the median leads to incorrect answers.