.sort() in python


#1

This code is from one of the codecademmy python excersizes:

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

for x in start_list:
    square_list.append(x**2)
    square_list.sort()

print square_list

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

[1, 4, 9, 16, 25]
None

Also please tell me if this should be in python instead of Corner Bar.
Thanks! :smile: :smile: :smiley_cat:


#2

Sort puts the numbers in order from smallest to largest.


#3

Does it only work with numbers?


#4

No, it goes alphabetically for strings.


#5

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


#6

@cadecodes

Ok I see, I was sort of expecting that.


#7

@stetim94

I will try.


#8

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:


#9

What parameters can you set it to?


#10

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.


#11

Whoa! Thats a little complicated.:grin:


#12

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


#13

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


#14

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


#15

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:


#16

Yep, complicated enough.


#17

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.


#18