Need help with "15. Median"


#1



Hi all. I've hit a bit of a roadblock with this exercise. I am receiving the error "list index out of range" with the attached code. It seems one of my week spots is indexes, and I don't appear to fully understand their use. Any help is greatly appreciated.


def median(lst):
    s = sorted(lst)
    if len(lst) == 1:
        return lst[0]
    elif len(s) % 2 != 0:
        j = (len(s) // 2) + 1
        return lst[j]
    else:
        k = lst[len(s) / 2]
        h = lst[k + 1]
        return (k + h) / (2.0)


#2

I figured it out. I was correct in that it was the indexes that were to blame. I neglected to remember that they start with 0 not 1...oops. my final code is below.