Lambda

A lambda statement is used to create new function objects. Essentially, the lambda takes a parameter followed by a single expression only which becomes the body of the function and the value of this expression is returned by the new function.

points = [ { 'x' : 2, 'y' : 3 },
           { 'x' : 4, 'y' : 1 } ]

points.sort(key=lambda i : i['y'])
print points
# output: [{'y': 1, 'x': 4}, {'y': 3, 'x': 2}]

Last updated