.. getthecode:: lambda.py :language: python3 :hidden: ==================== Lambda Expressions ==================== This page contains a memo on lambda expressions. A lambda expressions is an anonymous function: .. code-block:: py3 multiply_by_two = lambda x: x*2 print(multiply_by_two(2)) .. code-block:: none 4 which is equivalent to .. code-block:: py3 def multiply_by_two(x): return x*2 But permit shorter syntactic construction like .. code-block:: py3 unsorted = [(x, i) for i, x in enumerate((3, 5, 4, 1, 2))] print(unsorted) .. code-block:: none [(3, 0), (5, 1), (4, 2), (1, 3), (2, 4)] .. code-block:: py3 sorted_list = sorted(unsorted, key=lambda x: x[0]) print(sorted_list) .. code-block:: none [(1, 3), (2, 4), (3, 0), (4, 2), (5, 1)]