Understanding Lambda Functions in Python

What is a Lambda Function? A lambda function is a small anonymous function defined using the lambda keyword. It's often used for short, throwaway functions that are only needed temporarily. Basic Syntax- The syntax of a lambda function is: "lambda arguments: expression" -arguments: A comma-separated list of parameters. -expression: An expression that is evaluated and returned. Examples 1️⃣ Basic Lambda Function: "add = lambda x, y: x + y print(add(2, 3)) # Output: 5 " Here, lambda x, y: x + y is a lambda function that adds two numbers. 2️⃣ Lambda with map(): "numbers = [1, 2, 3, 4, 5] squared = list(map(lambda x: x ** 2, numbers)) print(squared) # Output: [1, 4, 9, 16, 25] " map() applies the lambda function to each item in the numbers list. 3️⃣ Lambda with filter(): "numbers = [1, 2, 3, 4, 5] even = list(filter(lambda x: x % 2 == 0, numbers)) print(even) # Output: [2, 4] " filter() uses the lambda function to filter out only the even numbers. 4️⃣ Lambda with reduce(): "from functools import reduce numbers = [1, 2, 3, 4, 5] product = reduce(lambda x, y: x * y, numbers) print(product) # Output: 120 " reduce() applies the lambda function cumulatively to the items in the list. Pros and Cons- Pros: -> Concise and readable. -> Useful for small, simple functions. -> Handy for functional programming (e.g., map, filter, reduce). Cons: -> Limited to single expressions. -> Can be less readable if overused. -> Lack of function name can make debugging harder. Lambda functions are an excellent tool for any Python developer to have in their toolkit. They can help streamline your code and make your functions more elegant and efficient.

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories