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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  pandas sum – Get Sum of Series or DataFrame Columns
  • 2.  Draw Rectangle in Python Using turtle Module
  • 3.  Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable
  • 4.  Using pandas resample() to Resample Time Series Data
  • 5.  Difference Between read(), readline() and readlines() in Python
  • 6.  Changing Python Turtle Size with turtlesize() Function
  • 7.  Using Python to Remove Last Character from String
  • 8.  Convert String to Datetime in pandas with pd.to_datetime()
  • 9.  Using Python to Check If List of Words in String
  • 10.  Remove Last Element from List Using 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