Learning about Lambda function in Python

Avinash Dalvi
4 min readMay 1, 2021

--

Few days back I was giving a session to engineering college students about the Python introduction explaining lambda function in python. I stuck that time while explaining details as I also know I am not expert for lambda usage in Python. But I thought this is the right time to learn in detail and share that learning with developers who are in the same condition as me.

Let’s understand what will gain from this article :

  • In details understanding about Lambda
  • how to use along with example

What is Lambda in Python ?

A Lambda Function in Python is an anonymous function or a function which doesn’t have any name. It is a mostly one liner and restricted function. It’s like a normal function, a Lambda function can have multiple arguments with one expression.

Creation of Lambda function :

For example :

lamba arguments(a,b,…n): expression
  • In general way in Python
def sum(a,b):
return a+b

print(sum(2,3))
Output : >>> 5
  • Using lambda in Python
sum = lambda a,b: a+b 
print(sum(2,3))
Output : >>> 5

In this above code the sum = lambda a,b: a+b is the lambda function. a and b are variables is the argument while a+b is the expression that is executed and the result of the expression is returned without return syntax. The expression gets the addition of the input parameters. Providing 2,3 as the parameters, which is added 2 and 3. Can see how generally the same logic can be written and in using lambda in terms of lines of code and execution wise.

But lines of code is the only reason to use Lambda in Python ? Is there any reason ? Answer is a big YES.

Lambda functions are used when you need a function for a short period of time. This is used when you want to pass a function as an argument to higher-order functions, that is, functions that take other functions as their arguments.

A higher order function is a function that takes a function as an argument, or returns a function. Higher order function is in contrast to first order functions, which don’t take a function as an argument or return a function as output. map, filter and reduce are higher order functions. Lambda works great as a parameter to higher order functions if you use it with its limitations

You may have noticed that, in the definition of the lambdas, the arguments don’t have parentheses around them. Multi-argument functions (functions that take more than one argument) are expressed in Python lambdas by listing arguments and separating them with a comma (,) but without surrounding them with parentheses.

  • Another way to call Lambda function is anonymously without assigning variable and passing arguments :
(lambda x, y: x + y)(2, 3)

Example,

def square(a):
return (lambda x: x*x)(a)

print(square(2))
  • Another example of calculating square for all list elements :
def square(x): 
return lambda : x*x

functions = [square(i) for i in [1,2,3,4,5,6]]
for function in functions:
print(function())
Output:
1
4
9
16
25
  • There is another concept called currying which means functions take up any number of calculations and data into a single real function that returns an expected output. Here is how I can convert the above square calculation example using currying.
functions = [lambda i=i: i*i for i in range(1, 6)]
for function in functions:
print(function())

In this currying example i=i is a positional argument. If you assign i=1 then all output will become 1. Here we are assigning i variable to i from iteration expression for i in range(1,6). As you can see we are not passing any argument from function() but in lambda declaring on the spot value assignment using i=i.

Another example for positional argument is :

In this example, z default value is 3. while passing arguments can't change position of 1st and 2nd argument. Firstly non-default argument should not follow the default argument means you can used it like (lambda x=2, y, z: x + y + z)(1, 2) it will throw error like below:

File "main.py", line 1 print((lambda x=2, y, z: x + y + z)(1, 2)) ^ SyntaxError: non-default argument follows default argument

It can be like this print((lambda x, y=2, z=3: x + y + z)(1)) because in this default value follow each others.

Conclusion Now you all know how to use Python lambda functions and some hints:

  • Write Python lambdas and use anonymous functions
  • Choose wisely between lambdas or normal Python functions
  • Avoid excessive use of lambdas

In this article I have not covered full advanced usage of lambda but I have tried to leverage understanding about basic lambda along with example. For more details below references can be used.

If any help is required about this article or more advanced understanding about Lambda can reach out to my twitter handler @aviboy2006.

References :

Originally published at https://www.internetkatta.com.

--

--

Avinash Dalvi
Avinash Dalvi

Written by Avinash Dalvi

Enthusiastic learner, Full Stack Developer, Techno Savvy, Traveller, Out of Box thinker, Agile Lover, Problem Solver, Blogger

No responses yet