Python lambda

23 Jun, 2022
In Python, an anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions.
Syntax:
lambda arguments : expression 
This function can have any number of arguments but only one expression, which is evaluated and returned.
One is free to use lambda functions wherever function objects are required.
You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
It has various uses in particular fields of programming besides other types of expressions in functions.
Example #1:
Python3
# Python program to demonstrate# lambda functions  string = 'GeeksforGeeks' # lambda returns a function objectprint(lambda string: string)
<function <lambda> at 0x7fd7517ade18>

In this above example, the lambda is not being called by the print function but simply returning the function object and the memory location where it is stored. So, to make the print to print the string first we need to call the lambda so that the string will get pass the print.
Example #2: 
Python3
# Python program to demonstrate# lambda functions  x = "GeeksforGeeks" # lambda gets pass to print(lambda x: print(x))(x)
GeeksforGeeks

Example #3: Difference between lambda and normal function call 
Python3
# Python program to illustrate cube of a number# showing difference between def() and lambda().  def cube(y):    return y*y*y  def g(x): return x*x*x  print(g(7)) print(cube(5))
343
125

Example #4: The lambda function gets more helpful when used inside a function.
Python3
# Python program to demonstrate# lambda functions  def power(n):    return lambda a: a ** n  # base = lambda a : a**2 get# returned to basebase = power(2) print("Now power is set to 2") # when calling base it gets# executed with already set with 2print("8 powerof 2 = ", base(8)) # base = lambda a : a**5 get# returned to basebase = power(5)print("Now power is set to 5") # when calling base it gets executed# with already set with newly 2print("8 powerof 5 = ", base(8))
Now power is set to 2
8 powerof 2 =  64
Now power is set to 5
8 powerof 5 =  32768

We can also replace list comprehension with Lambda by using a map() method, not only it is a fast but efficient too, and let’s also see how to use lambda in the filter().
Example #5: filter() and map()
Python3
# Python program to demonstrate# lambda functions inside map()# and filter()  a = [100, 2, 8, 60, 5, 4, 3, 31, 10, 11] # in filter either we use assignment or# conditional operator, the pass actual# parameter will get returnfiltered = filter (lambda x: x % 2 == 0, a)print(list(filtered)) # in map either we use assignment or# conditional operator, the result of# the value will get returnedmapped = map (lambda x: x % 2 == 0, a)print(list(mapped))
[100, 2, 8, 60, 4, 10]
[True, True, True, True, False, True, False, False, True, False]

simmytarika5
as5853535
adnanirshad158
rmohanraj91
python-lambda
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
Iterate over a list in Python