0 votes
in Education by (1.7m points)
This question already has answers here:

How are lambdas useful? [closed] (26 answers)

Closed 3 years ago.

Currently I am learning lambda functions in Python 3. Till now all I know about lambda functions is that its an anonymous function which can take any number of arguments but can contain only one expression.

So my question is that why do we need lambda functions if we can do the same using normal expression. I mean what was the actual purpose of introducing lambda functions?

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.7m points)
Occassionally you need a very simple function for a one-time use, e.g. to pass as a paramater to .sort() to tell it how to do the sorting. E.g. if you want to sort a list of tuples on their second element,

list_of_tuples.sort(key=lambda t: t[1])

is nicely concise, compared to

def first_element(t):

    return t[1]


And that is all.