15. Median PLEASE HELP


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#

How do you get rid of the [] square brackets around a list index. The error message was:
Oops, try again.
median([1]) returned [1] instead of 1


I don't know what I've done wrong. Please help me. Thanks in advance for anyone who helps.


def median(lst):
    sort = [sorted(lst)]
    
    if len(sort) == 1:
        return sort[0]
    
    elif len(sort) % 2 != 0:
        middle = len(sort) // 2 
        return sort[middle]
        
    elif len(sort) % 2 == 0:
        upper_middle = sort[(len(sort) + 1) // 2] 
        lower_middle = sort[len(sort) / 2 - 1]
        return sort[(upper_middle + lower_middle) / 2.0]


#2

this is wrong as sorted() already returns a list so correct is:
sort = sorted(lst)

also

this is incorrect as upper and lower_middle are already your list elements and not indexes, so you do not need to reference to the elements with sort[], it should be:
return (upper_middle + lower_middle) / 2.0


#3

Thanks a lot tomas! I actually never knew that sorted() returned a list. Now I've gotten past it now.