Files
30-seconds-of-code/snippets/median.md
Isabelle Viktoria Maciohsek 19f636408c Make expertise a field
2022-03-01 20:27:27 +02:00

805 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Median math beginner 2019-10-03T12:02:17+03:00 2020-11-02T19:28:27+02:00

Finds the median of a list of numbers.

  • Sort the numbers of the list using list.sort().
  • Find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even.
  • statistics.median() provides similar functionality to this snippet.
def median(list):
  list.sort()
  list_length = len(list)
  if list_length % 2 == 0:
    return (list[int(list_length / 2) - 1] + list[int(list_length / 2)]) / 2
  return float(list[int(list_length / 2)])
median([1, 2, 3]) # 2.0
median([1, 2, 3, 4]) # 2.5