For my first trinket post, here’s an example about lambdas and sorting:
This code defines a function that is then used as the sort key in the call to item.sort()
:
def get_first_item(x):
return x[0]
items.sort(key = get_first_item)
This is the same result as using a lambda like this:
items.sort(key = lambda x: x[0])
In this case, a lambda is more efficient to write if the function won’t be reused. The named function get_first_item
might be a good way to write this program if there were recurring need to sort by the first item of a sequence.
Overall, lambdas can be useful, but they’re very terse and only impact one portion of the code.