Finding median of list in Python
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The median is the middle value of an ordered data set, which makes it a useful summary when outliers would distort the mean. In Python, the best way to compute it depends on whether you want the simplest built-in tool, a manual implementation, or array-oriented performance with NumPy.
The Easiest Option: statistics.median
For normal Python lists, the standard library already provides exactly what you want:
This works for both odd and even list lengths:
For an odd number of items, the median is the center value after sorting. For an even number of items, Python returns the average of the two middle values.
This is the most readable solution and should be your default choice unless you have a specific reason not to use the standard library.
How to Compute It Manually
If you want to understand the logic or avoid an import, the algorithm is simple:
- sort the list
- find the middle index
- return the center value, or average the two center values
The important detail is using sorted(values) instead of values.sort() if you do not want to mutate the original list.
Using NumPy for Array Workloads
If your data is already in a NumPy array, numpy.median is the natural option:
NumPy is especially useful when median is part of a larger numerical workflow, or when you want to compute medians across an axis in a matrix:
That is much more convenient than manually slicing nested lists when you are doing data analysis or scientific computing.
Choosing the Right Approach
Use statistics.median for ordinary Python programs and scripts. Use a manual version if you are learning the algorithm or need full control over how the values are handled. Use NumPy when the data is already array-based or when you need axis-aware operations.
The mathematical definition stays the same in all three cases. The main difference is ergonomics and the data structures you are already working with.
Common Pitfalls
The biggest mistake is forgetting to sort the data before taking the middle element manually. The median is defined on ordered values, so taking the center of the original list is usually wrong.
Another common issue is handling even-length lists incorrectly. The median is not the lower middle or upper middle item by default; it is the average of the two central values.
Empty input is another edge case. statistics.median([]) and a sensible manual implementation should raise an error because there is no meaningful median for an empty data set.
Mixed non-numeric types can also cause trouble. If the list contains values that cannot be ordered or averaged together, median calculation will fail or produce results you do not want.
Summary
- '
statistics.medianis the simplest way to find the median of a Python list.' - A manual implementation works by sorting and selecting the middle value or middle pair.
- '
numpy.medianis a better fit for arrays and axis-based calculations.' - Remember that even-length lists use the average of the two middle values.
- Handle empty input explicitly instead of assuming a median always exists.
Related reading
- Finding neighbourhoods cliques in street data a graph
- Finding overlapping data in arrays
- Finding the average of a list
- Finding the best cosine similarity in a set of vectors
- Finding middle element of linked list with 1 pass, is this a creative useless answer?
- Finding Minimum Completion Time of Scheduled Tasks with Topological Sort
- Finding the index of elements based on a condition using python list comprehension
- Finding the source code for built-in Python functions?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.