• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Find Median of List in Python

Find Median of List in Python

May 5, 2022 Leave a Comment

To find the median number of a list in Python, you just need to sort the list of numbers and then find the middle number. If the length of the list is odd, you take the middle number as the median. If the length of the list is even, you find the average between the two most middle numbers.

def findMedian(list_of_numbers):
    list_of_numbers.sort()
    if len(list_of_numbers) % 2 == 1: 
        median = list_of_numbers[len(list_of_numbers) // 2]
    else: 
        median = (list_of_numbers[len(list_of_numbers) // 2 - 1] + list_of_numbers[len(list_of_numbers) // 2]) / 2
    return median

print(findMedian([9,3,1]))
print(findMedian([10,50,25,30]))

#Output:
3
27.5

When working with lists of numbers in Python, the ability to perform calculations and get certain statistics can be very valuable.

One such calculation and statistic is the median, or middle number, of a collection of data.

In Python, we can easily find the median of a list of numbers with a function.

To find the median of a list of numbers, we first need to sort the list.

Then, depending on if the length of the list is even or odd, we can get the median.

If the length of the given list is odd, we can find the middle index with integer division. If the length of the list is even, then we get the middle two indices with integer division.

Below are some examples of how you can find the median of a list in Python.

def findMedian(list_of_numbers):
    list_of_numbers.sort()
    if len(list_of_numbers) % 2 == 1: 
        median = list_of_numbers[len(list_of_numbers) // 2]
    else: 
        median = (list_of_numbers[len(list_of_numbers) // 2 - 1] + list_of_numbers[len(list_of_numbers) // 2]) / 2
    return median

print(findMedian([9,3,1]))
print(findMedian([10,50,25,30]))

#Output:
3
27.5

Hopefully this article has been useful for you to learn how to find the median in a list of numbers in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Capitalize Every Other Letter of String
  • 2.  Python atan – Find Arctangent and Inverse Tangent of Number
  • 3.  Replace Forwardslashes in String Using Python
  • 4.  Python power function – Exponentiate Numbers with math.pow()
  • 5.  Using Python to Remove Last Character from String
  • 6.  Run Something Every 5 Seconds in Python
  • 7.  Sign Function in Python – Get Sign of Number
  • 8.  Python rjust Function – Right Justify String Variable
  • 9.  Python Check if Object is Iterable with hasattr() Function
  • 10.  Concatenate Multiple Files Together in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy