Table of Content
- What are Lambda Functions in Python with an example?
- Why use Lambda Functions in Python
- How to Write Lambda Functions in Python
- How to Use Lambda Functions in Python with Code Examples
- Can you Call, Invoke or Reuse Lambda Functions in Python?
- How to Assign a Lambda Function to a Variable
- Wrap Off
One of the handy functions you'll get to use when working with the Python programming language is lambda functions.
Lambda functions in Python are what you might call anonymous functions in other programming languages.
Developers who use JavaScript and associated libraries are familiar with the term anonymous functions.
However, both lambda functions in Python and anonymous functions in JavaScript share the same concept.
In this post, we will explain what lambda functions are with examples of how to use them. In the end, you will learn what these functions do and how to use them to write better codes.
What are Lambda Functions in Python with an example?
In Python, lambda functions are nameless functions with a single expression as their body.
This means that they cannot be reused after writing them since they cannot be imported into a module.
In most cases, they are usually written in a single line.
Why use Lambda Functions in Python
Lambda functions, or anonymous functions as they are sometimes called, are a very important part of programming.
They come in very handy when you need to write a function quickly to show an expression.
They are also useful when you need to make a function that is immediately invoked when you run a program for the first time.
Those are not the only reason you may use these functions.
The usefulness of lambda functions in Python comes to light when combined with a higher-order function, that takes in other functions as arguments.
For example, when we combine it with the map()
, filter()
, and reduce()
functions in Python.
How to Write Lambda Functions in Python
You can write Lambda functions in the Python programming language by using the lambda
keyword.
Syntax of Lambda Function in python
The code below is an example of the syntax of a lambda function in python:
lambda <arguments> : <expression>
The body of a lambda function in python must be a single expression.
Again, this must be a code expression in python, not a code statement.
It is essential to understand the difference between an expression and a statement in python codes and programming in general.
This will save you tons of debugging time and to write better codes with less coupling and high cohesion, especially when using lambda functions in Python.
For starters, an expression returns a value, a statement does not return anything, except maybe, None, in the case of Python.
It is imperative to know how to define and write lambda functions.
The simplest example of a lambda function is a function that performs a quick mathematical operation on a number.
Here is a simple example of how to use lambda functions in Python.
This function simply takes an argument and calculates its square.
lambda num : num * 2
If you are wondering if lambda functions in python accept two parameters.
The answer is yes. Lambda functions can take more than one argument as well.
lambda a, b : a * b
Example of Lambda function with filter()
The filter()
function in Python takes in a function and a list as arguments.
It gets called on all the items in the list and returns a new list containing items for which it evaluates to True
.
Here is an example of using the filter()
function along with the Lambda function to select even numbers from a list in Python.
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(filter(lambda x: (x % 2 == 0) , my_list))
print(new_list)
Example of Lambda function with map()
The map()
function in Python takes in a function and a list.
It is called on all the items in the list and returns a new list containing items returned by that function for each item.
Here is an example use of the map()
function with Lambda function to double all the items in a list.
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
new_list = list(map(lambda x: x * 2 , my_list))
print(new_list)
Can you Call, Invoke or Reuse Lambda Functions in Python?
You cannot call or invoke Lambda functions directly, but you can assign them to variables.
And to mitigate their lack of reusability, sort of, you can define multiple lambda functions in python or create a sequence.
How to Assign a Lambda Function to a Variable
The code below takes two arguments, sums both, and assign them to a variable
add = lambda a, b : a + b
print(add(27, 2)) # 29
Wrap Off
There are many benefits of lambda functions.
You may not know or appreciate the usefulness until you come across a requirement when coding in Python, where it just makes sense to use.
If you enjoyed this post or learned something, please consider sharing and signup to our weekly email to get awesome posts delivered to you weekly.