Lambda Functions
Small anonymous functions written in a single expression.
Easy
square = lambda x: x * x
components = [("R1", 220), ("C1", 100), ("R2", 10)]
components.sort(key=lambda c: c[1]) # sort by value
Lambdas are best for short throw-away functions passed to sorted, map, filter or callbacks. If it needs a name or more than one line, use def.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.