Python Lambda
Python Lambda Functions are anonymous functions meaning that the function is without a name. As we already know that the def keyword is used to define a normal function in Python. Similarly, the lambda keyword is used to define an anonymous function in Python.
For Example
lambda : print(‘Hello World’)
Here, we have created a lambda function that prints ‘Hello World’.
Before you learn about lambdas, make sure to know about Python Functions.
Python Lambda Function Syntax
Syntax: lambda arguments : expression
- This function can have any number of arguments but only one expression, which is evaluated and returned.
- One is free to use lambda functions wherever function objects are required.
- You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
- It has various uses in particular fields of programming, besides other types of expressions in functions.
The anatomy of a lambda function includes three elements:
- The keyword lambda — an analog of def in normal functions
- The parameters — support passing positional and keyword
arguments, just like normal functions - The body — the expression for given parameters being evaluated
with the lambda function
Note that, unlike a normal function, we don’t surround the parameters of a lambda function with parentheses. If a lambda function takes two or more parameters, we list them with a comma.
We use a lambda function to evaluate only one short expression (ideally, a single-line) and only once, meaning that we aren’t going to apply this function later. Usually, we pass a lambda function as an argument to a higher-order function (the one that takes in other functions as arguments), such as Python built-in functions like filter(), map(), or reduce().
Examples
- A simple lambda function
Let’s start with a basic example. We’ll create a lambda function that calculates the square of a number.
square = lambda x: x ** 2
You can call this lambda function by passing an argument like this:
result = square(5)
print(“The square is:”, result)
- Lambda function in a list
Lambda functions are often used with built-in functions that accept a function as an argument, such as map, filter, and sorted. Here’s an example using map to apply a lambda function to a list of numbers:
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
The map function applies the lambda function to each element in the numbers list, producing a new list of squared numbers.
- Lambda function for sorting
Lambda functions are handy when you need to customize the sorting order of a list of objects. In this example, we sort a list of dictionaries based on the ‘age’ key:
people = [
{‘name’: ‘Alice’, ‘age’: 30},
{‘name’: ‘Bob’, ‘age’: 25},
{‘name’: ‘Charlie’, ‘age’: 35}
]
sorted_people = sorted(people, key=lambda x: x[‘age’])
The sorted function uses the lambda function as the sorting key to sort the list of dictionaries by age.
- Filtering with lambda
Lambda functions can be used with the filter function to create custom filters. Here’s an example that filters out even numbers from a list:
numbers = [1, 2, 3, 4, 5, 6]
filtered_numbers = list(filter(lambda x: x % 2 != 0, numbers))
The lambda function filters out numbers that are not even, resulting in a list of odd numbers.
Conclusion
Lambda functions are a powerful tool in Python for creating small, ad-hoc functions without the need for formal function definitions. They are commonly used with higher-order functions like map, filter, and sorted to perform custom operations on data. With the examples provided in this blog post, you should have a good foundation for using lambda functions effectively in your Python projects. Practice and experimentation will help you become more comfortable with this concise and versatile feature.