Tutorial by Examples

An anonymous, inlined function defined with lambda. The parameters of the lambda are defined to the left of the colon. The function body is defined to the right of the colon. The result of running the function body is (implicitly) returned. s=lambda x:x*x s(2) =>4
Map takes a function and a collection of items. It makes a new, empty collection, runs the function on each item in the original collection and inserts each return value into the new collection. It returns the new collection. This is a simple map that takes a list of names and returns a list of the...
Reduce takes a function and a collection of items. It returns a value that is created by combining the items. This is a simple reduce. It returns the sum of all the items in the collection. total = reduce(lambda a, x: a + x, [0, 1, 2, 3, 4]) print(total) =>10
Filter takes a function and a collection. It returns a collection of every item for which the function returned True. arr=[1,2,3,4,5,6] [i for i in filter(lambda x:x>4,arr)] # outputs[5,6]

Page 1 of 1