LAMBDA :

Syntax

Where to use:

MAP :

Syntax

FILTER :

Syntax

PRACTICE PROBLEM :

Problem: Input a list of numbers and print the sum of all even elements in that list.

Here's a one liner solution using the 3 magical keywords :

Python provides several keywords which enable a functional programming approach for python. These functions are all convenience features in that they can be written in Python fairly easily and can be replaced by custom code with some more line of code. As most of the beginners to python are confused or do not efficiently use these features, I will here try to explain those features in a simple manner.Lambda function also called anonymous function or unbounded function can be used as any normal function in python but without any name.Starting with the keyword lambda followed by parameters before the colon and return value after it.Ex: Getting any number modulo 3 ( of-course this can be done using simple %3 )is equivalent toThe above python expression can be stated as "declare a nameless function taking a parameter named x. Perform the operation x%3. The return value of this nameless function will by the result of this operation." A lambda function can take any number of arguments and returns the value of a single expression.Those things are actually quite useful. Python supports a style of programming called functional programming where you can pass functions to other functions to do stuff like map, filter, reduce, with tkinter objects, etc.These can be used as any other function as a python object(every thing in python is an object) like passing to a function, return from a function, assigning it to variables, etc.Should be used where the function is one liner, non reusable, where you do not want to name another function and pollute the name-space.This keyword is taken from Google's concept of map-reduce in Big Data.This is a function that 'maps' the given elements of a list to certain function.The elements of the list are sent as parameters, implicitly, to that function one by one and the return value is saved in another list which is returned.Ex: Replacing all elements of a list to result of the element modulo 3can be written asIt should be used as it is generally more fast than writing a for loop and applying the function to its elements.expects n-argument function for n sequences.Note: In python3, it returns an iterable so it should be explicitly converted to a list using list() function call.It is another element of functional programming in python. It is used to filter out or choose only those elements from a list which satisfies certain condition. The condition are either True or False and should be returned by a function or a lambda expression should be used.Ex: Get all even elements in a list.Note: In python3 it returns a iterable so it should be explicitly converted to a list using list() function call.