15. Median


Been scratching my head over this... :confused:


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

def median(lists):
    sort = sorted(lists) 
    la = len(sort) 
    if la % 2 != 0:
        n = la / 2 - 1
        return lists[n]
        nex_mid = la / 2
        prev_mid = nex_mid - 1
        ave = (lists[prev_mid] + lists[nex_mid]) / 2.0
        return ave


hi @heartogrin,

la will be an integer since it calculates the length of a list. When you take la divided by 2, you will get another integer as a result, rounded down if needed. This will give you the wrong index.

For example, if your list has 5 elements,la will be 5. The middle element, by manual calculations or observation, can be determined to be the element index 2. But when you do n = la / 2 - 1, your result will be 1, which is the wrong index.

Try looking for an alternative pattern and coming up with another math formula :slight_smile:

Let me know if you need more hints


Thanks for a quick reply.

You're right!! I didn't realise this. Would it help if I do this:
n = int(la / 2)

The next step would be figuring out how to make the average of the even no. of numbers. I need some light on this.


Nope, that won't work as well. As I mentioned earlier, when you do la / 2, you will already get an integer, so there is little point in using the int type converter function again thereafter :stuck_out_tongue:

Let us try to spot the pattern.

When you have a list of 5 elements, the middle element is of index 2.

What can a possible link between 5 and 2 be?

When you have a list of 7 elements, the middle element is of index 3.
When you have a list of 9 elements, the middle element is of index 4.

What can the possible links be?

Hint: consider the respective indexes multiplied by 2. E.g. 2 * 2 = ? , 3 * 2 = ? , 4 * 2 = ?

Let me know how it goes :slight_smile:


I've had a completely crazy idea but here goes...

n = (la - 1) / 2


Excellent idea! :slight_smile:

Why not give it a shot eh


Alright. I've updated it to my code and still get same error message as this: Oops, try again. median([4, 5, 5, 4]) returned 5.0 instead of 4.5


Okay @heartogrin,

look at your error message. It says that the list of [4, 5, 5, 4] returned the wrong value. When you count the number of elements in this list, you realise that this list has an even number of elements, which will cause the else statement to be executed instead. This means that there is also an error hidden in the else bit!

The error is right here. Why did you use lists when calling for the indexes? Remember you did this at the start:

So now, your new sorted list which your subsequent code should refer to should be sort, not lists.

Give it another shot, and see if you get what I mean :slight_smile:

P.S. Spot the same fatal flaw in your if statement!


Fixed two flaws and now everything is sort-ed!!

Thanks @umopapsidn, you've been a great help to me! :grinning:


Nice pun there, @heartogrin :stuck_out_tongue:

You're welcome! You can mark that this post has been solved by checking the checkbox beneath any of the above posts :slight_smile:


Oh and @heartogrin,

One last thing in case you're interested. The same code can be compressed to this form:

def median(no):
    no = sorted(no)
    if len(no) % 2 != 0:
        return no[(len(no)-1)/2]
        return (no[(len(no)/2)-1] + no[len(no)/2]) /2.0

All the best!


Thanks, @umopapsidn.

Just one question. Is it better for all codes that should to be compressed?

Cos uncompressed codes are so much easier for me to read and understand, and it's awkward for me to see all codes clamped together in a single line.


I do not think that good codes must be compressed, but just a matter of curiosity :slight_smile:

Personally I like uncompressed codes better too, and they are less error-prone.


Jesus, I must learn to read the instructions TO THE END. If I did with this one, I would divide by 2.0 and not 2 straight away.


Will you please post the final code that worked?
I can't find the answer.