15, Median-I don't know what am I doing


This is my code:

def median(med_value):
    med = []
    value = str(med_value)
    total = 0
    for i in value:
        x= sum(med)
        y = str(len(value))
        total = total + y / x
    return total

it returns error:
median([1]) resulted in an error: unsupported operand type(s) for +: 'int' and 'str'
What am I doing wrong?
Thank you.


A string cannot be divided by an integer, only sliced.

I would like to speak about the variable names. Statistics has some fairly well defined concepts, and using variable names that reflect that is far more useful than just inventing them.

A sample space is a collection of data surveyed from a sample population. The median is the middle value of a sorted sample space. When two values fall in the middle, they are averaged. So I wouldn't begin by suggesting that the median is a list. It's a value.

Given that we are handed a list, we should not have value in the variable name. It is not semantic. When we describe the inputs we self-document the code.

    def mean(sample_space):
        return float(sum(sample_space)) / len(sample_space)

Applied in this case...

>>> def median(sample_space):
        x = sorted(sample_space)
        n = int(len(x)/2)
        return x[n] if len(x) % 2 else float(x[n-1] + x[n]) / 2

>>> median([5,5,4,4])
>>> median([5,5,6,4,4])


How do I do that?Is there any specific keyword in Python that can allow me to do that?


Took me a while to edit the above, but the answer may be in there.

Let's examine the code in the OP...

def median(med_value):
    med = []
    value = str(med_value)
    total = 0
    for i in value:
        x= sum(med)
        y = str(len(value))
        total = total + y / x
    return total

We know that the first line is removed.

Given that the parameter is a list, the second line will raise an exception. We need to use string formatting to represent a list as a string. The loop that follows assumes an iterable. A list is one already.

for i in med_value:

Here we stall. Problems solved means long explanations. Sometimes just looking at it solves it. Let's give that a try, first, eh?


Hello everyone,

I am so happy that I managed to do the median code without help and I would like to share my happiness with everyone here, so here is my very own code:

def median(median_value): #my very own code
    median_value = sorted(median_value)
    print median_value # I removed a for loop, which turned out, it was not necessary for this case.! 
    if len(median_value)% 2 == 0:
        middle_2 = len(median_value)/2
        middle_index_1 = median_value[middle_2]
        middle_index_2 = median_value[middle_2-1]
        middle_odd = (middle_index_1 + middle_index_2)/2.0
        return middle_odd
    elif len(median_value)% 2 != 0:
        middle_1 = len(median_value)/2
        middle_index = median_value[middle_1 ]
        return middle_index
print median([5,68,12,13,2,9,1,8, 4, 50])
print median([1, 2, 5, 8, 9 ,7 ,5])



Thank you for the solution,but I wanted to do it while I learn how to do it.Atleast I can study from your code and try and understand the task.Thanks.


@arwaahmed Great job!

As I'm reading these forums, I'm noticing a lot of people are using a for loop to iterate over the items in the list and I'm not exactly sure why. Is there a reason people are using for loops?

@bakikovac your function seems to be finding the average and not the median.

lst = [1,2,3,4,100]
The median of this list will be 3, since it is the middle item of the list.
The average would be 22.


As I am new, my answer might not be very satisfying but I will try my best to explain how I understand the for loop.
- I only used codecademy to practically learn Python, so starting from early lesson you will build confidence about loops and how and when to use each. But I think so far for loop is the most used because it is easier to understand and it gets your code work.
- for loop is very suitable if you are working with lists, dictionaries, strings or any type of series data as it allows you to iterate through the length of that data, through each item and also allows you to run the code contentiously until it reach the last item in "say" a list. for example you can make it sounds like this: for each item in my_list please divide each item by 2 and print the results or return the result, for loop will carry this mission until it reaches that last item in you list . with a lot of practice, you will have the feeling that for loop is the most understandable loop in python programming.
I hope this was helpful.


Thanks @arwaahmed!

In this instance do you need to iterate over the items?

Seems to me a quick rule of thumb should be, if you're never accessing the i in a (for i in list) for loop, that you really don't need it.

Here's the code that worked for me:

def median(lst):
    if len(lst) % 2 == 0:
        med = (len(lst)/ 2)
        return (lst[med] + lst[med - 1])/2.0
        med = (len(lst)/2)
        return lst[med]


Wow.!! thank you for pointing that out for me, as I am new to coding, I don't really see the shortest and efficient way for coding.
Your code is super simple and easy. Thank you for sharing that.
Best of luck with coding.