How to use custom code to find the median of a list in Python

1 Answer

0 votes
def median(lst):
    half = len(lst) // 2
    lst.sort()
    if not len(lst) % 2:
        return (lst[half - 1] + lst[half]) / 2.0
    return lst[half]
    
 
lst = [1, 7, 12, 5, 8, 9, 8] 
 
print(median(lst))



 
'''
run:
  
8
   
'''

 



answered Apr 21, 2021 by avibootz
...