Wednesday, November 12, 2014

Map, Reduce and Filter in Python

Python Supports Functional Programming using Lambda. Python treats functions as first-class objects. It can be used as variables.


>>> def sum(x,y) :
...     return x + y
... 
>>> sum(2,4)
6
>>> mysum = sum
>>> mysum(34,45)
79
>>> sum
<function sum at 0x7f6e5c7135f0>
>>> mysum
<function sum at 0x7f6e5c7135f0>


Lambda allows us to create a short "Anonymous" functions. For example "(lambda x, y: x+y)(3,4)" will return 7.

In this post, we will be looking into 3 function(filter, map, and reduce) from an functional point-of-view:
  • filter(filtering function object, list) 
  • map(mapping function object, list) 
  • reduce(reducing function object, list) 

Filter function:
The filter() call filtering function for each element in the given list and selects the element based on the return value of the filter function.


>>> num = range(1,100)
>>> filter(lambda x: x%5 == 0, num)
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
>>>


Map Function:
The map() function applies mapping function for each element in the given list and returns the mapped resultant list.


>>> num10 = range(1,11)
>>> num10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> square10 = map(lambda x: x**2, num10)
>>> square10
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>


Maps are really useful when you want to extract a particular field from an array of Dictionary.

>>> dictList = [ {'FirstName': 'Michael', 'LastName': 'Kirk', 'SSID': '224567'},
... {'FirstName': 'Linda', 'LastName': 'Matthew', 'SSID': '123456'},
... {'FirstName': 'Sandra', 'LastName': 'Parkin', 'SSID': '123456'},
... {'FirstName': 'Bob', 'LastName': 'Henry', 'SSID': '666666'},
... {'FirstName': 'Silvia', 'LastName': 'Perkin', 'SSID': '676767'}]
>>> dictList
[{'LastName': 'Kirk', 'SSID': '224567', 'FirstName': 'Michael'}, 
{'LastName': 'Matthew', 'SSID': '123456', 'FirstName': 'Linda'}, 
{'LastName': 'Parkin', 'SSID': '123456', 'FirstName': 'Sandra'}, 
{'LastName': 'Henry', 'SSID': '666666', 'FirstName': 'Bob'}, 
{'LastName': 'Perkin', 'SSID': '676767', 'FirstName': 'Silvia'}]
>>> map(lambda x: x['LastName'], dictList)
['Kirk', 'Matthew', 'Parkin', 'Henry', 'Perkin']
>>> 


Reduce Function:
The Reduce function applies the reducing function for the first pair of the list and then applies reducing function  again with the resultant with the next element in the list, so on till all the last element of the list and returns the last resultant.


>>> num = [2, 4, 5, 3, 7, 9, 8, 3, 1]
>>> reduce(lambda x, y: x if (x > y) else y, num)
9
>>>
>>> reduce(lambda x, y: x if (x < y) else y, num)
1
>>> reduce(lambda x, y: x + y, num)
42
>>> reduce(lambda x, y: x * y, num)
181440
>>>>>> def factorial(num):
...     return reduce(lambda x, y: x * y, range(1, num+1))
...
>>> factorial(1)
1
>>> factorial(2)
2
>>> factorial(3)
6
>>> factorial(4)
24
>>>
>>> def getCount(num, list):
...     return reduce(lambda x, y: x+y, map(lambda x: 1 if x == num else 0, list))
...
>>> getCount(5, [1,2,3,4,5,6,7])
1
>>> getCount(5, [1,2,5,4,5,6,7])
2
>>>