#!# ====================
#!#  Lambda Expressions
#!# ====================
#!#
#!# This page contains a memo on lambda expressions.

#!#
#!# A lambda expressions is an anonymous function:

multiply_by_two = lambda x: x*2

print(multiply_by_two(2))
#o#

#!# which is equivalent to

def multiply_by_two(x):
    return x*2

#!# But permit shorter syntactic construction like

unsorted = [(x, i) for i, x in enumerate((3, 5, 4, 1, 2))]
print(unsorted)
#o#

sorted_list = sorted(unsorted, key=lambda x: x[0])
print(sorted_list)
#o#

3.3.9. Lambda Expressions

This page contains a memo on lambda expressions.

A lambda expressions is an anonymous function:

multiply_by_two = lambda x: x*2

print(multiply_by_two(2))
4

which is equivalent to

def multiply_by_two(x):
    return x*2

But permit shorter syntactic construction like

unsorted = [(x, i) for i, x in enumerate((3, 5, 4, 1, 2))]
print(unsorted)
[(3, 0), (5, 1), (4, 2), (1, 3), (2, 4)]
sorted_list = sorted(unsorted, key=lambda x: x[0])
print(sorted_list)
[(1, 3), (2, 4), (3, 0), (4, 2), (5, 1)]