Python Lambda
The lambdas are anonymous one line functions in Python that can be used to perform tasks which don't require regular Python functions. Some of the most useful features of Python lambdas are...
- Lambda function can take any number of arguments
- Lambda functions are very short although can be very encryptic
- Lambda functions can be used to return function objects
- Lambda functions are restricted to only a single expression
Below snippet shows the basic syntax of a Python function. The below function takes "argument" and returns it back.
# normal function that returns a value
def functionName(argument):
return argument
Lambda Function Definion
Example of above function "functionName" using Lambda
lambda argument : argument
Note the syntax of the above function. Lambda functions have no name. They are defined and used on the fly. We can't reuse them in the form defined above. The first 'argument' is the argument of the function and the 2nd 'argument' is the returned value.
Example: - Lambda function that returns the twice of the given input.
lambda x : x * 2
But you must be wondering, how to provide the input to above lambda function!
Calling the Lambda Function
To use the function, you can surround it with parenthesis and provide the parameters between paranthesis as shown below.
(lambda x : x * 2) (5)
This is how it works...
# (lambda x: x * 2 )(5) = lambda 5 : 5 * 2 = 5 * 2 = 10
There is another way to provide an argument to the lambda function. We can assign the lambda function to a variable and then pass the argument to that variable as shown below...
double = lambda x : x * 2
# and call it like so :
double(5)
Ofcourse above lambda function and below function are equivalent.
# the function double is equivalent to :
def double(x):
return x * 2
Lambda Functions With Mutiple Arguments
Python Lambda functions can have multipe parameters separated by commas (,) here is an example...
pairs = lambda x , y : "P( x = "+str(x)+" , y = "+ str(y)+" )"
pairs(1,2)
There is a shorthand way of calling Python lambdas function that is without assigning a name to the function.
You can simply do this :
lambda a , b : a * b
There will be no conflict and you can call the above lambda function with the arguments like this...
_(2,3)
Note the underscore in the above syntax. Underscore refers to the lambda function that we just described above.
Ofcourse, you can use IIFE ( Immediately Invoked Function Expression ) syntax too.
(lambda a , b : a * b) (2,3)
We can use function inside lambda. Below snippet is an example of lambda function inside another lambda function.
# we can use a function as a parameter of lambda :
myfunction = lambda param , func : param + func(param)
# and call it like so :
myfunction(3,lambda x : x**2)
In the above snippet, we passed outer lambda function two parameters - param and another lambda function (func)
myfunction(4,lambda x : x - 1)
import dis
div = lambda x,y : x / y
type(div)
dis.dis(div)
div
# applaying same thing for a normal function:
import dis
def div(x,y):
return x / y
type(div)
dis.dis(div)
div
Lambda functions throws similar errors as Python regular functions do. For example, below snippet will throw string multiplication error "can't multiply sequence by non-int of type 'str'"
type_error = lambda str1,str2 : str1 * str2
type_error("hello","world")
We can't add statments in lambda function as shown below.
(lambda x : assert x > 0)(1)
However we can use parenthesis to achieve the statement effect.
In below snippet, note expression (x>0 and + or '-') would translate to if x > 0 then return '+' otherwise return '-'
(lambda x : (x>0 and '+' or '-'))(-5)
Hinting doesn't work on lambda functions. It only works on normal functions.
Below regular Python function takes 'string' and 'integer' as two parameters but returns output as string.
def function(param:str,i : int)-> str:
return param * str(i)
In lambda function, if you specify type hints, you will end up getting a syntaxError...
lambda param:str , i : int : param * i
*args and **kwargs in Python Lambda
As we described above in the 'multiple arguments section' of this post, Python lambda function can take multiple arguments but lambda functions can also take arguments using *arg
and **kwargs
(lambda p1 , p2 , p3 : (p1 + p2 + p3)/3)(1,2,3)
(lambda p1 , p2 , p3 = 3 : (p1 + p2 + p3)/3)(1,2)
*args example
(lambda *args : sum(args)/len(args))(1,2,3)
**kwargs example
(lambda **kwargs : sum(kwargs.values())/len(kwargs))(one = 1, two = 2, three = 3)
Python lambdas initialize arguments example
(lambda p1 , p2=0 , p3=0 : (p1 + p2 + p3)/3 ) ( 1 , p2=2 , p3=3)
Let us see first, how decorators in regular Python functions work. Here is an example...
# Defining a decorator
def trace(f):
def wrap(*args, **kwargs):
print(f"[TRACE] function name: {f.__name__}, arguments: {args}, kwargs: {kwargs}")
return f(*args, **kwargs)
return wrap
# Applying decorator to a function
@trace
def double(x):
return x * 2
# Calling the decorated function
double(3)
Check out below example of application of decorator to a lambda function. Notice in the below snippet, how we have wrapped the lambda function inside the trace decorator function. Order of brackets is very important.
print((trace(lambda x: x * 2))(3))
Lambda is regularly used with built-in functions like map or filter.
map iterates the function through a list or set. The function could be regular Python function or lambda function.
In the below example,lambda function x: x + 5 is applied on list of numbers (0,4)
list(map(lambda x : x + 5 ,range(5)))
In the below example,lambda function x: x > 0 is applied on list of numbers [-5,-2,1,0,3,5]
list(filter(lambda x : x>0,[-5,-2,1,0,3,5]))
In below snippet, map() is taking two arguments. The first one is the decorator function around lambda function i.e.
trace(lambda x: x * 2) and second argument is range(3).
The map() will run the decorated lambda function 3 times as shown below.
list(map(trace(lambda x: x * 2), range(3)))
import unittest
double = lambda x : x * 2
class Test(unittest.TestCase):
def test_1(self):
self.assertEqual(double(1),2)
def test_2(self):
self.assertEqual(double(2),4)
def test_10(self):
self.assertEqual(double(10),11) # this test will fail
if __name__ == '__main__':
unittest.main(verbosity=2)
double = lambda x : x * 2
double.__doc__ = """Doubles the number entred in the parameters :
>>> double(1)
2
>>> double(2.0)
4.0
>>> double(10)
20
"""
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)
Here is how regular Python function raises exception.
def throw(excep):
raise excep
Here is how lambda functions can raise exception.
(lambda : throw(Exception("my error")))()
Let us look at below example. In here (lambda : * 2) _ refers to a variable or parameter.
(lambda _ : _ * 2)(11)
Let us take at following more cryptic code example. In below code, there are two Python lambda functions. The parameter _ is being used inside both lambda functions.
(lambda _ : list(map(lambda _ : _ * 2,_)))(range(11))
# the underscore (_) is the variable
# to simplify, the function should be like this :
(lambda myList : list(map(lambda element : element * 2,myList)))(range(11))
Here is how it works.
lambda 0 : list(map(lambda : *2,0)) \ lambda 0 : list(0) \ 0
lambda 1: list(map(lambda : *2, 1)) \ lambda 1: list(2) \ 2 \ ....
Here is the above code in a regular Pythonic way...
# regular functions will make it easy due to their multiline format
def doubleItems(myList):
double = lambda x : x * 2
return map(double , myList)
list(doubleItems(range(11)))
# let's sort a list of strings that have a char a space and an int based ont he value of the integer
myList =["a 1","b 45","x 11","r 16"]
print(sorted(myList))
print(sorted(myList,key = lambda x : int(x[2:])))
from timeit import timeit
timeit(lambda :sum(range(99999)), number=10)
# this silution is cleaner and more readable
Related Notebooks
- How to do SQL Select and Where Using Python Pandas
- Summarising Aggregating and Grouping data in Python Pandas
- Merge and Join DataFrames with Pandas in Python
- How to Convert Python Pandas DataFrame into a List
- How to Plot a Histogram in Python
- How to Generate Random Numbers in Python
- Python Pandas String To Integer And Integer To String DataFrame
- How to Upgrade Python PIP
- Most Frequently Asked Questions Python Pandas Part1