What is Lambda?
Lambdas, also known as anonymous functions, are small, restricted functions which do not need a name (i.e., an identifier). Lambda functions were first introduced to the field of mathematics by Alonzo Church in the 1930s.
Today, many modern programming languages like Java, Python, C#, and C++ support lambda functions to add functionality to the languages.
In this Lambda tutorial, you will learn:
- What is Lambda?
- Lambdas in Python
- Syntax and Examples
- Using lambdas with Python built-ins
- lambdas in filter()
- lambdas in map()
- lambdas in reduce()
- Why (and why not) use lambda functions?
- Lambdas vs. Regular functions
In Python, lambda expressions (or lambda forms) are utilized to construct anonymous functions. To do so, you will use the lambda keyword (just as you use def to define normal functions).
Every anonymous function you define in Python will have 3 essential parts:
- The lambda keyword.
- The parameters (or bound variables), and
- The function body.
A lambda function can have any number of parameters, but the function body can only contain one expression.
Moreover, a lambda is written in a single line of code and can also be invoked immediately. You will see all this in action in the upcoming examples.
printer 1 REGULAR CALL
printer 2 REGULAR CALL
printer 1 LAMBDA CALL
lambdas in reduce()
The reduce function, like map(), is used to apply an operation to every element in a sequence. However, it differs from the map in its working. These are the steps followed by the reduce() function to compute an output:
Step 1) Perform the defined operation on the first 2 elements of the sequence.
Step 2) Save this result
Step 3) Perform the operation with the saved result and the next element in the sequence.
Step 4) Repeat until no more elements are left.
It also takes two parameters:
Why (and why not) use lambda functions?
As you will see in the next section, lambdas are treated the same as regular functions at the interpreter level. In a way, you could say that lambdas provide compact syntax for writing functions which return a single expression.
However, you should know when it is a good idea to use lambdas and when to avoid them. In this section, you will learn some of the design principles used by python developers when writing lambdas.
One of the most common use cases for lambdas is in functional programming as Python supports a paradigm (or style) of programming known as functional programming.
It allows you to provide a function as a parameter to another function (for example, in map, filter, etc.). In such cases, using lambdas offer an elegant way to create a one-time function and pass it as the parameter.
When should you not use Lambda?
You should never write complicated lambda functions in a production environment. It will be very difficult for coders who maintain your code to decrypt it. If you find yourself making complex one-liner expressions, it would be a much superior practice to define a proper function. As a best practice, you need to remember that simple code is always better than complex code.
As previously stated, lambdas are[vV4][J5] just functions which do not have an identifier bound to them. In simpler words, they are functions with no names (hence, anonymous). Here is a table to illustrate the difference between lambdas and regular functions in python.