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
  3. Hello today, together with a friend found a bombed little sylochku to entertain and to spend fun time Lucky online casino canada do you want as well as we have fun come in

    ReplyDelete
  4. Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
    python training in bangalore

    ReplyDelete
  5. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.
    Data science Course Training in Chennai |Best Data Science Training Institute in Chennai
    RPA Course Training in Chennai |Best RPA Training Institute in Chennai
    AWS Course Training in Chennai |Best AWS Training Institute in Chennai

    ReplyDelete
  6. Attend The Analytics Courses in Bangalore From ExcelR. Practical Analytics Courses in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Analytics Courses in Bangalore.
    ExcelR Analytics Courses in Bangalore

    ReplyDelete
  7. I have gain a lot of information's from your Articles and Looking For more Posts...Thanks for this Information's about Python
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

    ReplyDelete

Post a comment

Popular posts from this blog

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

Privacy Protection Algorithms

Research Paper Summary - Detecting Outliers in Categorical Data