Lambda Functions
1. What is a Lambda Function?
A lambda function in Python is a small anonymous function defined using the lambda
keyword. Unlike regular functions defined with def
, a lambda function is created on the fly and is often used when a function is needed for a short period of time, typically as an argument to higher-order functions (like map
, filter
, and sorted
).
The syntax of a lambda function is:
- arguments: One or more parameters that the function will take.
- expression: A single expression that will be evaluated and returned. The expression cannot contain statements or assignments.
2. Basic Example of Lambda Function
Here’s a basic example of a lambda function that adds two numbers:
In this example,lambda x, y: x + y
is a lambda function that takes two arguments x
and y
, and returns their sum.
3. Lambda Functions in Higher-Order Functions
Lambda functions are commonly used with built-in functions like map()
, filter()
, and sorted()
.
Examples
The map()
function applies a given function to all items in an iterable (e.g., list).
Example:
lambda x: x**2
is used to square each number in the list.
The filter()
function filters elements of an iterable based on a condition defined by a function.
Example:
lambda x: x % 2 == 0
checks if a number is even.
The sorted()
function can take a key argument, which is often a lambda function, to sort the elements based on a specific criterion.
Example:
lambda x: x[1]
.
4. Lambda Functions with Default Arguments
You can also define lambda functions with default arguments. Here’s an example:
5. Lambda Functions with Multiple Arguments
Lambda functions can take multiple arguments. Here’s an example of a lambda function that calculates the product of three numbers:
6. Lambda Functions in List Comprehensions
You can combine lambda functions with list comprehensions to perform more complex operations in a concise way.
Example:
7. Why Use Lambda Functions?
- Conciseness: Lambda functions allow you to write small, one-off functions in a single line, making your code more concise and easier to read when the function is simple.
- Functional Programming: Lambda functions are widely used in functional programming paradigms, where functions are often passed as arguments to other functions.
8. When Not to Use Lambda Functions
- Readability: If the lambda function is complex or spans multiple lines, it’s better to define a regular function using
def
. It improves readability and makes debugging easier. - Lack of Name: Since lambda functions are anonymous, it’s harder to debug them as they don’t have meaningful names.
9. Lambda Functions in Classes
Although lambda functions are often used for quick tasks, they can also be used within classes, especially as short methods or as part of custom sorting or filtering functions.
Example:
10. Conclusion
Lambda functions are a powerful feature in Python for creating concise, functional code. While they’re great for short, throwaway functions, you should avoid using them in more complex situations where readability is important.