Map, Reduce and Filter

This is my first post here at codelog and all my code snippets for this post would be written in python even though the topic is mostly language agnostic. This is a fairly common feature found popularly in functional programming languages like lisp.

Before we see examples of map-reduce-filter, I would have to explain about lambda functions(which I would be using a lot) – borrowed from lisp.

Lambda Functions

Lambda functions are nameless functions which can be used primarily to define one line functions which need to be passed around. Lambda functions are written using the lambda keyword.

>>> lambda x: x*2
<function <lambda> at 0x81d617c>

This lambda function takes in one argument x and returns twice the input number. This may seems too simple at first, but believe me you can easily write complex stuff using these. You can store these lambda functions and/or pass them around as parameters to other functions.

Map

Map is a function which takes in two arguments – first is a function and second is an array (or list). What it does is simple. It applies the function over all elements of the list and returns another list.

>>> map(lambda x: x*2, [1,2,3,4])
[2, 4, 6, 8]

This doubles all the elements in the list. Here is another example which takes in a list of tuples and adds the two elements in each tuple and creates another list.

>>> map(lambda x: x[0]+x[1], [(1,2), (3,4), (5,6)])
[3, 7, 11]

You can do real cool stuff using these.

Reduce

Reduce also takes in two parameters like map – one is a function and another is a list. But it instead applies the function on two successive parameters from the list. This is useful for adding all the elements in a list.

>>> reduce(lambda x, y: x+y, [1,2,3,4,5,6,7,8,9,10])
55

First it adds 1 and 2, then it adds the result to the next element 3, whose result is added to 4 and so on. This is pretty simple, but you have to keep in mind that the function that you pass in to reduce function takes in 2 parameters.

Filter

Filter is a function which also takes in a method and a list – but returns back another list with all elements which return True when passed to the method. Might seem confusing first – but as the word says it filters out the elements of the list which matches the condition.

>>> filter(lambda x: x%2 == 0, [1,2,3,4,5,6,7,8])
[2, 4, 6, 8]

The lambda function there is very simple. It just checks whether the number is odd or even. It returns True if it is even and False if it is odd. Passing it to a filter function with a list of numbers generates a list with only elements which pass the filter – even numbers.

In Python 3 (which is to be released very soon almost 991 years in advance) there will be slight changes to these four functions. Maybe I should make it as another post in which we will see how we can do the same stuff using list comprehensions – which is the suggested method.

Tags: ,

4 comments

  1. Good job. Welcome to Codelog. Nice to see python posts under CodeLog.

    Regarding the lambda functions, I would like to add a sentence. Even C# has lambda functions (which I was surprised to hear about). I will write a posts about lambda functions in c# very soon.

  2. Yeah, I too came to know about that today while listening to this week’s StackOverFlow podcast.

  3. thx 4 d your description. soHElpfull

Leave a comment