.sort() in python

This code is from one of the codecademmy python excersizes:

start_list = [5, 3, 1, 2, 4]
square_list = []

for x in start_list:

print square_list

I don’t quite understand what .sort() does.
The output is:

[1, 4, 9, 16, 25]
Also please tell me if this should be in python instead of Corner Bar.
Thanks! :smile: :smile: :smiley_cat:

Sort puts the numbers in order from smallest to largest.

1 Like

Does it only work with numbers?

1 Like

No, it goes alphabetically for strings.

1 Like

Someone (or multiply people) wrote a function called sort, which allowed you to sort things based on number/strings on alphabetic order. If you want a better understanding of sort, you can write your own, the easiest two are merge and bubble sort if i am not mistaken, i am sure you can find tutorials for them online

1 Like

@cadecodes[quote=“cadecodes, post:4, topic:22978, full:true”]
No, it goes alphabetically for strings.
Ok I see, I was sort of expecting that.

1 Like


I will try.

1 Like

Actually sorting in Python can sort by basically any criterion you can think of, like length of the elements or stuff like that; just set the key parameter and you are done :slightly_smiling:

1 Like

What parameters can you set it to?

1 Like

Your fantasy is the limit: you can set the key parameter to whatever you like (normally as a lambda).

For example, if pippi is a list of strings:

sorted(pippi, key=lambda a: -ord(a[0])*len(a))

would sort it taking the first char in reverse alphabetical order and multiplying its value by the length of the string itself.

On a side note, the current sorting algo is a hybrid (for efficiency), using mostly the merge sort (usally the best option for large arrays/lists), IIRC.


Whoa! Thats a little complicated.:grin:

1 Like

You really have no idea how complicated I can get when I do competitive coding :wink:

1 Like

Really? Wow.:open_mouth: What is competitive coding?:confused:

compete against other programmers to solve a problem in the best possible way, i think, not sure

Precisely, stetim.

And, uh, let’s say I have built a reputation elsewhere for juggling with an insane number of instructions into my (un)famous one-liners solutions :wink:

Like this:

function_for_a_kata_will_not_name=lambda c: lambda p,t: (lambda i,n: 'Invalid present value' if p<0 else 'Invalid duration' if t%1!=0 else 'Invalid rate frequency' if n==None else 'Invalid interest rate' if i==None else round(p*((1+i/n)**(n*(t-(t%(1/n) if n<1 else 0)))-1),2))(float(c["rate"][:-1])/100 if (lambda r: len(r)>1 and r[-1]=="%" and r.count(".")<=1 and all(x in "0123456789" for x in r.replace(".","").replace("%","")))(c["rate"]) else None,{"daily":365, "weekly":52, "monthly":12, "quarterly":4, "annually":1, "biennially":0.5}[c["frequency"]] if c["frequency"] in ['daily', 'weekly', 'monthly', 'quarterly', 'annually', 'biennially'] else None)

Believe it or not, this stuff is a single line that works to solve a given problem and is EXCELLENT practice to improve your skills :slightly_smiling:

So, is this complicated enough for you :smiley:? I hope so :stuck_out_tongue:


Yep, complicated enough.

1 Like

henlo friend. i notice that you enjoy NARUTO. how do i know. i have a LONG boy and he is I N T E L L I - G E N T.