How they work - The 3 Magical Functions of Python : map, filter and lambda

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 :

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.
Syntax - lambda arguments: expression
Ex: Getting any number modulo 3 ( of-course this can be done using simple %3 )
is equivalent to
The 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.
Where to use:
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.

MAP :

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.
Syntax : map(function, sequence)
Ex: Replacing all elements of a list to result of the element modulo 3
can be written as
It should be used as it is generally more fast than writing a for loop and applying the function to its elements.
map 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.

FILTER :

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.
Syntax : filter(function, sequence)
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.

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 :

Comments

  1. Most of the time, it's better to use a list comprehension than map/filter, it's more readable and more pythonic.


    l = [1, 2, 3, 4, 5]
    even = [i for i in l if i % 2 == 0]
    new_l = [i % 3 for i in l]
    new_l_even = [i % 3 for i in l if i % 2 == 0]

    ReplyDelete
  2. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    Python Training in Bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Privacy Protection Algorithms

Natural Language Interface for Relational Database - What is it and How it can be made?