You can use the list.sort
to avoid creating new lists with sorted
and sort the lists in place.
Also you should not use list
as a variable name as it shadows python's own list.
def median(l):
half = len(l) // 2
l.sort()
if not len(l) % 2:
return (l[half - 1] + l[half]) / 2.0
return l[half]