What is the lambda function?


What does it mean to call the lambda function "anonymous"?
What exactly is the lambda doing?


Have a google search
== discussions / opinions ==
python what is lambda function site:stackoverflow.com
for instance

== the Book ==
lambda function site:python.org


def known():

This is now a defined function. It is not anonymous because it has a name.

print (list(map(lambda x: x[0] + x[1], [(1,2),(3,4),(5,6),(7,8),(9, 10)])))

# [3, 7, 11, 15, 19]

Here we have used an anonymous function in place, in an off the cuff manner. That's where lambda is the ultimate corkscrew. We can pull one out in any instance without need of a defined function. It can be a one time thing and we get the information we need.

To better understand the function, here,

return x[0] + x[1]

is the function (return is implied, not literal). The lambda is being called as the map() function cycles through the list of tuples it is given; and list() is unwrapping the returned object.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.