
You might have came across certain lambda function in python a few times online particularly while interacting with Python programs and might be wondering what a lambda function is and what its application is in Python programming. However, we are going to clear your confusion around this topic of how we are using and implementing lambda functions in Python programming.
In this article, we will hover around some of the major specifics of Lambda function and its uses in Python programming getting a complete overview around the topic.
Python Lambda function or Lambda function in Python is commonly referred to as an anonymous function wherever you want to create a function that contains only expressions to execute. It is generally a single line of code expression which can easily perform a simple calculation with an argument to another function when needed. You can provide any number of arguments in the Python Lambda function.
Let us understand how we can write Python lambda function syntax easily in programming. It is a single line code expression which can be used to execute specific tasks in a program.
|
lambda arguments: expression |
| add = lambda x, y: x + y print(add(5, 3)) |
| square = lambda x: x * x print(square(4)) |
The Python Lambda function is very useful in certain conditions when we are writing a Python program.
You can use lambda function to create simple expressions which do not include complex structures such as if else, for loops and so on in the program. You can use the lambda function with iterables.
|
filter ( function, iterable) |
|
map (function, iterable) |
| list1 = [3, 4, 6, 8, 9] result = list(map(lambda x: x * 2, list1)) print(result) |
| list1= [6, 8, 12, 16, 18] |
| from functools import reduce
reduce(function, iterable) |
| from functools import reduce numbers = [1, 2, 3, 4, 5] sum_result = reduce(lambda x, y: x + y, numbers) print(sum_result) # Output: 15 |