.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.


Does it only work with numbers?


No, it goes alphabetically for strings.


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



Ok I see, I was sort of expecting that.



I will try.


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:


What parameters can you set it to?


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:


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


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.


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.