15. median


#1

Does anyone have an idea why this doesnt work with lst = [1]?


def median(lst):
    sort = sorted(lst)
    if len(lst) % 2 == 0:
        value = (sort[len(lst)/2] + sort[(len(lst)/2)+1])/2
    else:
         value = sort[(len(lst)/2)+1]
    return value


#2

cause above index will be: (1/2) + 1 = 0.5 + 1 = 1.5 so 1 as integer and in your sort list there is no item with index of 1 (as you have only 1 element with index of 0)


#4

Thanks! I have already found out :slight_smile: