15. Median


I made this to work, but somehow I think it's not the easiest way and I believe it might be bugged at some point.

Practise makes it perfect. 15. Median

def median(x):
    sort = sorted(x)
    iteminsorteda = 0
    iteminsortedb = 0
    iteminsortedc = 0
    medianinsorteda = 0
    medianinsortedb = 0
    medianinsortedc = 0
    realmedian = 0
    if len(sort) % 2 == 0:
        iteminsorteda = len(sort) / 2
        iteminsortedb = len(sort) / 2 - 1
        medianinsorteda = sort[iteminsorteda]
        medianinsortedb = sort[iteminsortedb]
        realmedian = (medianinsorteda + medianinsortedb) / (2.0)
        return realmedian
    elif len(sort) % 2 > 0:
        iteminsortedc = len(sort) / 2
        medianinsortedc = sort[iteminsortedc]
        return medianinsortedc


I dont see any mistake - code is fine IMHO.
Your way is absolutly ok - and it is an easy way!

It's possible to shorten it, if you dont use that many variables.
First of all you can skip the whole part setting them to 0 since you define them later in code before using them.
Then you can skip some of them if you want to make code more compact.
For example:

elif len(sort) % 2 > 0:
        iteminsortedc = len(sort) / 2
        medianinsortedc = sort[iteminsortedc]
        return medianinsortedc

could be written:

        return sort[len(sort]/2]

the if-branch can be shortened too of course :slight_smile:


Thanks a lot :slight_smile:
Yeah, now I can see at least some places where I could've shorten my code... Guess I'll need a lot of practise to be able to think in shorter codes before I write them :smiley:


Shorter code is not necessary better code!
And shorter code is not necessary faster / more performant.

You should try to write code, that can be understood easy and that is easy to maintain.

for example, its totally ok to use helper-variables like iteminsorteda and iteminsortedb (maybe the names are not the best choice).
but instead of writing

iteminsorteda = len(sort) / 2
iteminsortedb = len(sort) / 2 - 1

IMHO a better way would be (regardless of the variable-names)

iteminsorteda  = len(sort) / 2
iteminsortedb  = iteminsorteda  - 1

the if-branch would look like:

if len(sort) % 2 == 0:
        iteminsorteda = len(sort) / 2
        iteminsortedb = iteminsorteda - 1
        return (sort[iteminsorteda] + sort[iteminsortedb]) / 2.0


Since I'm new to coding at all it is quite surprising for me that shorter code would not necessary be more performant :confused:

Shortened code makes a lot of sense. Thanks a lot, appreciate your tips!


Since you are new to programming - i think performance shouldnt be something you should care at the moment. :grinning:
Thats something to worry about if you are very experienced and are working on large projects.


Hi! inter2K3!

I have a question, how could you shorten the if-branch?
I tried and am getting an error:

edit: Thanks! i'm really finding this forum Q/A highly helpful for this course. I really appreciate all the help, guys and ladies!


i think i solved it but i have a new issue with the else statement. i had to remove it and start over.
edit: weird, it seems that my quoted text does not indent the correct way. I'm not sure how to quote things here i guess.
Sorry for the confusion:

The issue is that i'm trying to shorten both my if statement and change the elif or shorten it. i managed to fix my if statement but now I am having trouble fixing my elif statement. should i use something besides elif?


I FIGURED IT OUT! hahaha! awesome.

thanks everyone! :3

basically i discovered that you can find the length of list "x" using first sorted to sort it, then put that into a new list "new_x". Next you find the length using len and put that into "n". You can now use "n" to shorten your codes. Just refer to it. so I subbed it in and reduced my code down to like one line statements! how awesome is that?!

It was good idea to write it out long form then go back and see where i can shorten my code. Simplify it down.

A lot of mistakes are stupid errors. So I do need a lot of practice.


def median(num):
length = len(sort)
if length % 2 == 0 :
new_list= (sort[length / 2] + sort[(length / 2)-1]) / 2.0
return new_list
elif length==1:
return new_list
new_list= sort[len(sort)/2]
return new_list


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