Oops, try again. median([4, 5, 5, 4]) returned 2.0 instead of 4.5

def median(x):
    y = sorted(x)
    for i in range(0, len(y)):
        if len(y) % 2 == 0:
            f = len(y)/2
            s = len(y) - f
            m = (f + s)/2.0
            return m
        elif len(y) == 1:
            return float(y[0])
        elif len(y) > 2:
            return len(y)/2

I feel like It should work, but I don't seem to be getting anywhere.


f and s are indexes of the list, not values of the list

use f and s as indexes to get values from the list, add them together and divide by 2.0


well i fixed that...i think. Now it's returning 5 instead of 4.5.

if len(y) % 2 == 0:
f = y[len(y)/2]
s = y[len(y) - f]
m = (f + s)/2.0


but now f contains a value from the list, not an index. So, you can't use f to calculate s


I have tried with this code . I am getting the correct output but it is displaying the output as error. Can anyone help me. I have tested many cases. But I am getting the same error.


but the exercise will also call the function:


but will not call your sorting function. So, you should call your sort function inside your median function


I have called the sorting with in median function itself. I have got the answer also.But still it is showing error.
I have called the function. It is behind the warning notification. So you were not able to see. sorry.Uploading...

def median(z):
value = 0
if len(z)%2 == 0:
value= (z[n] + z[m])/2.0
else :
value = z[m]
return value
def sorting(x):
index =0
for item in x:
index +=1
for i in range(index,len(x)):
if item>= x[i]:
dup = x[i]
x[i] =item
return x

print median(sorting(y))


Hi @stetim94 I have got the output. I have called the sorting function with in the median function . Now i got the correct output. Thanks. Earlier i have called it directly and assigned the value while calling the funtion. I t was the mistake. :slight_smile: :slight_smile: :slight_smile:


Pardon me for butting in. I couldn't help but notice that there was quite an accumulation of code, all in the aim of solving a simple problem.

We are after the median, which is the value at the middle point in a sorted sample space (data). When there are an even number of data points, then the middle two are averaged out, and that becomes the median. In either case the median is a value that represents the middle data point.


If the list contains even length then it is getting the value of average of Middle Values. I have represented by doing the average of (z[n]+z[m])/2 . if the length is odd I have directly given the middle value represented by z[m]. @mtf . Please notify me if I am mistaken anywhere. Thanks. Then output is also correct .


I'm only astounded by the nearly twenty lines of code. Surely that can be refined.


Just forward me if there is any other simple code. I have added extra lines for clear understanding. Even I feel it can be minimized few more steps. Thanks for the help.


Given a sorted sample space, it has a length.

n = len(x)

With zero-indexing, half of the length will give the index of the lowest value in the top half. One less than that index will be the highest value of the lower half.


Can you please forward me the full fledged code of what you are describing. It would be really helpful for me in the future. I feel you were explaining Quick sort method. Isn't it?


def median(lst):
index = 0
print lst
for lst_index in range(len(lst)):
print lst_index
if lst_index % 2 == 0:
index = lst_index/2
print index
print lst[index]
return lst[index]
index = lst_index / 2
print index ,"Index"
print lst[index],"first index value"
print lst[index+1],"secod index value"
x= lst[index]
median_value = (x+y)/2.0
return median_value


Nothing so sophisticated as that. The median problem can be solved with very little code.

The middle index (or indices, as apply) is the integer of length divided by 2.

n = len(lst)

m = int(n / 2)

Odd length,

int(9 / 2)  => 4

0 1 2 3 4 5 6 7 8

Even length

0 1 2 3 4 5 6 7
      ^ ^


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