WRITELOOP

PYTHON LAMBDAS (SIMPLY EXPLAINED)

2014 July 10

“lambda” is a way to create a one-liner function (an “anonymous” function). Example: Traditional way: def main(): … … y = square(some_number) … return something def square(x): return x2 The lambda way: def main(): … square = lambda x: x2 y = square(some_number) return something Together with “map” it is a powerful way to do fast operations on lists, for example. Maps takes 2 arguments: one function and a iterable. Example: a = [1,2,3,4] squared_list = map(lambda x: x**2, a) Lambdas (“lambda” keywork) and Mapping (“map” keyword) are a powerful way to manipulate data. If used wisely it can be beautifully pythonic. Reference: http://stackoverflow.com/questions/890128/python-lambda-why