Lesson 11 of 55 · Python
Lambda Functions
Duration: 20 minutes
Lesson 11 of 55 · Python
Duration: 20 minutes
lambda args: expression.\n\npython\nsquare = lambda x: x * x\nprint(square(5)) # 25\n\n# Sorting a list of tuples by the second element\npeople = [(1, 'b'), (2, 'a'), (3, 'c')]\nsorted_people = sorted(people, key=lambda item: item[1])\nprint(sorted_people)\n\n\nBecause a lambda can only contain a single expression, they’re best used as callbacks for functions like sorted, map, or filter.\n