Lambda Function In Python With Examples
One of the best features of Python is that it is one of the most intuitive programming languages, making it easier for developers to create code. However, there are still certain concepts that a beginner can find difficult to grasp and comprehend. One such concept is lambda. The lambda function in Python is an anonymous function that can contain only a single expression.
Many think that lambda functions as an intermediate or advanced concept in Python functions. However, this tutorial blog will help you understand what is lambda function, its syntax, how to use with Python built-ins, and when to use it.
What is Lambda Function in Python?
The lambda functions are similar to user-defined functions but are anonymous with no name. It is typically a small and restricted function that can’t have more than one line.
As a developer, you can use lambda expression in Python to construct anonymous functions in Python. To do the same, you will have to use the lambda keyword. Moreover, every anonymous function defined in the Python programming language will have three essential parts- the lambda keyword, the parameters, and the function body.
Python Lambda Function Syntax
Now that you know what a Lambda function is in Python, let’s look at the formula syntax for writing a Lambda function.
Lambda argument (s): expression
Here,
1. ‘lambda’ is a keyword in Python that is used to define the anonymous function.
2. ‘argument (s)’ is a placeholder or parameters – a variable that can be used to hold the value that is to be passed into the function expression. Always remember that there can be multiple variables for a lambda function depending on what you want to achieve.
3. ‘expression’ is the code that a developer wants to execute using the lambda function.
Points to be Remember:
- The anonymous function will not have a return keyword as it will automatically return the result of the expression in the Lambda function after its execution.
- Moreover, it can have enormous parameters, but there will be only one expression in the function body.
- No brackets are to be used around the ‘argument (s)’ or parameters, unlike regular functions.
To learn more about Python Lambda function, consider opting for a comprehensive Python course.
Python Lambda Function Examples and Syntax
After learning about lambda function theoretically, let’s use lambda function in Python example:
adder = lambda x, y: x + y
print (adder (1, 2))
>> 3
Explaining the Code
Here, the output is a variable that is returned by the this function.
1. The lambda keyword is used for defining the anonymous function.
2. x and y parameters are passed to Python’s lambda function.
3. This is the body of the function that contains two parameters passed by the developer. It is to be noted that the body contains a single expression, not multiple statements.
4. After applying the lambda function, we will have the returned value.
Using Lambda with Python Built-ins
Lambda functions make it easier to perform operations using built-in methods in Python programming language. The reason for this is that Lambdas are callable as soon as they are defined and passed as an argument to Python’s built-in functions. Here you will learn the use of the Lambda function in Python.
Lambda Function with Filter()
The filter() function in Python offers an elegant and powerful way to select some particular elements from a sequence of elements. The sequence is typically an iterator, such as sets, lists, tuples, and any other iterator. Furthermore, a filter function in Python will require another function containing the expression or operation that is to be performed on the iterator.
For example,
In this example, all odd numbers are filtered out using the filter() function and lambda function.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x % 2 != 0), li))
print(final_list)
Here is the output:
[5, 7, 97, 77, 23, 73, 61]
Lambda Function with Map()
The map() function is used for applying a particular operation to each value in a sequence so that they can be modified.
For example,
In this example, all the elements of the list are multiplied by 2 using the map() function and lambda function.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2, li))
print(final_list)
Here is the output:
[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
Lambda Function with Reduce()
Similarly to the map() function, the reduce() function is also used for applying a particular operation to each value in a sequence so that they can be modified. However, the working of reduce function is quite different from the map function. This particular built-in function of Python performs a repetitive operation over the pairs given in the sequence.
For example,
In this example, the sum of all elements of the list is calculated using reduce() function and lambda function.
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print(sum)
Here is the output:
193
When and Why We Use Lambda Function in Python
Python uses a style of programming referred to as functional programming. Lambdas enable a developer to provide a function as a parameter to another Python built-in function (for instance, in the map, filter, reduce, etc.).
In such a case, the Lambda function will make it simpler to create a one-time function and pass it as the parameter. Therefore, always prefer using the Lambda function when you want to create simple expressions that don’t include complex structures, like for-loops, if-else, and so on.
Lambda vs. Regular Functions
The following are the differences between the lambda functions and the regular functions in Python:
Def defined Functions (Regular Functions) | Lambda Functions |
---|---|
The keyword def is used to define the regular function, and it needs a function name in the local namespace. | The keyword lambda is used to define the lambda function, and it doesn’t necessarily need a function name in the local namespace. |
Regular Python functions can contain an enormous number of execution statements inside the function definition. | It allows performing limited operations. |
These functions are easy to interpret. | These functions can take time to interpret. |
It’s important to define the return explicitly to return the object from the particular function. | The program for the same operation is executed much faster. |
Conclusion
Lambda functions in Python are small and restricted anonymous functions that don’t need a function name as an identifier. There are three key parts of every lambda function – the lambda keyword, the arguments (parameters), and the expression (function body). These functions can be used with Python built-in functions, including a map, reduce, and filter.
FAQs
In Python, DEF is used to define normal/named functions whereas lambda is used to define an anonymous function.
Some benefits of lambda functions are:
* They are short and concise.
* They are flexible and reusable.
* They help in avoiding cluttering of code.
Lambda functions are one line functions used to create short and reusable codes that are not part of the main program. They are also used to pass functions as arguments to other functions.
The main characteristics of lambda functions are:
– They are anonymous functions as they do not have any names.
– They can take multiple arguments, but return only a single expression.
– They are used with higher order functions.
– They help to create short, concise functions.
Built-in functions and user-defined functions are the two main types of functions in python.