[Tutor] Sorting tuples

Alan Gauld alan.gauld at btinternet.com
Thu Jun 21 02:13:31 CEST 2012


On 21/06/12 00:27, Mike Nickey wrote:
> While i'm still not sure what lamda is or represents,

lambda is a function without a name.

>> def sort_last(tuples):
>>     # +++your code here+++
>>     print sorted(tuples, key=lamda tuples: tuples[-1])
>>     return

The lamda above is identical to doing:

def f(t):
    return t[-1]

print sorted(tuples, key=f)


And in general:

def f(arg):
    return expression

can be written:

f = lambda arg: expression

And they are most usually used, as in the case of sorted, where its 
convenient to define the function in place rather than having small 
functions scattered around which might only be used once.

The term lambda comes from the Lambda calculus which is one of the 
underlying mathematical models that programming is based upon. (Along 
with state automata theory, Boolean algebra, relational theory etc)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list