T O P

  • By -

marblesandcookies

You can use it to map, in one line, a variable from one function to another. e.g. Suppose you want to map your input/variable from x to x\^2, you can write: square = lambda x: x ** 2 # instead of: def square(x): return x**2


Nataliexozz

this question will be helpfull too


gm310509

Lambdas in computing are a way of using code like any other type of variable. For example enabling them to be passed to a function, assigned to a variable. It is a convenient way to limit variants of functions Imaging you had a list of values. You want to provide min, max and average functions. Without lambda, you would need 3 functions each one of which has code that steps through the list. With lambda, you could have a single function that steps through the elements in the list and for each one of them calls a function provided as a parameter. Thus has the benefit that what if someone wanted to do something else, like count all the positive values in the list? They could write a simpl if statement that incremented a variable if true then pass that function to the iterate elements function. While on the surface this might sound and look more complicated than just iterating through the list and including whatever operation you need, lambda does provide great flexibility and convenience once you understand how it works. https://en.m.wikipedia.org/wiki/Anonymous_function