I keep getting the following error message "Oops, try again. median([4, 5, 5, 4]) resulted in an error: list index out of range"

I'm probably missing something obvious here, but I'm just not seeing it.

def median(unsorted_list):
    legnth = len(unsorted_list)
    new_list = sorted(unsorted_list)
    if legnth == 1:
        return unsorted_list[0]
    elif legnth % 2 == 0:
        one = new_list[legnth / 2]
        two = new_list[one - 1]
        result = (one + two) / 2.0
        return num
    elif legnth % 2 != 0:
        new_list = new_list[:-1]
        two = new_list[one - 1]
        result = (one + two) / 2.0
        return result 

Thanks in advance!


example below for the list [4,5,5,4]

this line:

one = new_list[legnth / 2]

one doesn't contain an index, it contains the number of the list, in this case 5, so here:

two = new_list[one - 1]

5 - 1 is index 4, which is out of range, this should be index 1. i would simply divide legnth by 2 and then subtract one


Since your variable has the same misspelling, it isn't raising any exceptions, but I believe you meant to write:

length = ...

Consider a sample space with an odd number of items, as in odd length. The single item list would fit this category and does not need its own special case, so this is not needed:

We have one of two return possibilities. Either the middle term of an odd length sorted sample, or the average of the middle two terms if the length is even. This is a binary problem when we boil it down. One or the other.

This suggests that the simplest conditional won't even need an else or elif.

if ____:
    return ...
return ...

Now just narrow down the return statements and you're done.

It's nice to see things broken down with the use of variables, but it becomes moot very quickly. There are no badges for verbose code, but if it helps you understand the components of the problem, then go ahead and use them. But expect yourself to go back and simplify your work. Don't just settle for the first thing that works. You'll learn more by revising and refining your code than you will ever learn if you stop there.

For this problem we only need three working variables.

  1. The sample space given in the parameter, sorted.
  2. The size or length of the sample space.
  3. The midpoint(s) of the sorted sample.

Adding more variables just complicates things, in my view, but as I said, if it helps in setting up the solution that's okay. Just go back and simplify afterward.

The sample space we already have, and if we do not have a need to preserve its original order, we can sort it in place without creating another list.

n = len(sample)
m = int(n / 2)

That's all we need to complete the solution. Now just write the returns and you're done.


Thanks a lot for that, I see what you mean about the variables and I've cleaned it up a bit.
It's still not working and I think its to do with how I'm finding the midpoints of the list.

Here's what I have now:

def median(sample):
    n = len(sample)
    m = (n / 2)
    x = (m -1)
    if n % 2 != 0:
        return m
    return (m + x) / 2.0


m and x are indexes, to get the number at this indexes you need to use sample[m]


Adding to what @stetim94 has pointed out, permit me to suggest that you write the float explicitly, rather than implicitly.

float(sample[x] + sample[m]) / 2

2 is a counting number (natural) and renaming as a float is rather misleading. If there is to be a float in this rational, it will be in the numerator so that is where to declare it.


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