# 15. Median Error: median([1]) returned [] instead of 1

#1

``````def median(lst):
x=len(lst)/2
if len(lst)%2==0:
first=lst[int(x-1):int(x)]
second=lst[int(x):int(x+1)]
return (first,second)
else:
mid=lst[int(x-0.5):int(x+0.5)]
return (mid)``````

#2

for lists with a odd numbers of items the length divided by 2 gives the index you need for the middle item

#3

Thank you.
But i can't get this one: median([4, 5, 5, 4]) returned 5.0 instead of 4.5
there are two numbers in the middle (5 and 5), so there is the question
Why it should return 4.5 instead of 5?

#4

from the information of the exercise:

The median is the middle number in a sorted sequence of numbers

look at the word i made bold, rather important, so `[4,5,5,4]` would be `[4,4,5,5]` after being sorted, then the two middle numbers are 4 and 5

@terapro42417, i am helping you, don't go hijack someone else his topic, it is against the guidelines, and you won't be helped faster.

#5

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