15.15 - Median - Uncertain indexing procedures, incorrect median returned


<img src="//codecademy-discourse.s3.amazonaws.com/original/5X/0/c/2/e/0c2e503da2fc627bc3347949fc06c4cbf4b32408.png" width="690" height="388">

def median(list):
    if len(list) % 2 == 1:
        x = list.index(((len(list)+1)/2))
        x = list.index(((len(list)/2) + ((len(list)/2)+1))) / 2
    return x

Oops, try again. median([1]) returned 0 instead of 1


why would you use .index()? this method gives you the first first index of matching value in list, while you want to do exactly the opposite, get the value from the middle index, general syntax:

value = yourList[index]


Median should be central index value. How else would I locate it? (Also, why does my screenshot not show? It showed on previous threads with the alt+prt sc -> ctrl+v shortcuts.)


what is index value?

I prefer to keep those terms separated, the list has indexes (starting at zero for the most left item) and values, for example for [4,5,5,4] the values are 4,5,5,4 while the indexes are 0, 1, 2, 3

you need the middle value (not index!) from the list as final result


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.