Inside the Briefcase

Augmented Reality Analytics: Transforming Data Visualization

Augmented Reality Analytics: Transforming Data Visualization

Tweet Augmented reality is transforming how data is visualized...

ITBriefcase.net Membership!

ITBriefcase.net Membership!

Tweet Register as an ITBriefcase.net member to unlock exclusive...

Women in Tech Boston

Women in Tech Boston

Hear from an industry analyst and a Fortinet customer...

IT Briefcase Interview: Simplicity, Security, and Scale – The Future for MSPs

IT Briefcase Interview: Simplicity, Security, and Scale – The Future for MSPs

In this interview, JumpCloud’s Antoine Jebara, co-founder and GM...

Tips And Tricks On Getting The Most Out of VPN Services

Tips And Tricks On Getting The Most Out of VPN Services

In the wake of restrictions in access to certain...

Map, Reduce and Filter: A Review of Python

August 7, 2018 No Comments

Featured article by Kaylan Sudhakar, Independent Technology Author

In recent times Python has become predominant language for Data Analysis.

Python provides several functions which enable a functional approach to programming.

The functools module is for higher-order functions: functions that act on or return other functions.

In this article, I will be presenting some use cases for map, filter and reduce functions which are also called as higher order functions (a function that takes in other functions as arguments).

Before we look at these functions lets understand lambda and list comprehensions.

Lambda:

Lambda is an anonymous function. Lambda functions can have any number of arguments but only one expression. In Python, anonymous function is a function that is defined without a name.

While normal functions are defined using the def keyword, in Python anonymous functions are defined using the lambda keyword.

Syntax of Lambda Function 

lambda arguments: expression

 

# Program to show the use of lambda functions

double = lambda x: x * 2

print(double(5))

# Output: 10

# Program to show the use of lambda functions

f = lambda a,b: a if (a > b) else b

print(f(1,2))

# Output: 2

The above program finds the maximum value of 2 given numbers

In Python, we generally use it as an argument to a higher-order function Lambda functions are used along with built-in functions like filter(), map() etc.

List Comprehensions:

List comprehension is the easiest way to create lists

Syntax of List Comprehension 

new_list = [expression(i) for i in old_list if filter(i)]

 

# Program to show the use of list comprehension

S = [x**2 for x in range(10)]

print (S)

#Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

In the above example for each value of x, x**2 is calculated. We can add a condition to this statement to include on values greater than 7.

 

# Program to show the use of list comprehension with filter

S = [x**2 for x in range(10) if x > 7]

print (S)

#Output: [64, 81]

In the above statement only 8,9 satisfy the condition

 

Map

One of the common things we do with list and other sequences is applying an operation to each item and collect the result. This can be easily achieved by map function. Since it’s a built-in, map is always available and always works the same way. It also has some performance benefit because it is usually faster than a manually coded for loop.

Syntax of map Function 

map(aFunction, aSequence)

The map function applies a passed-in function to each item in an iterable object and returns a list containing all the function call results.

# Program to show the use of map function

items = [1, 2, 3, 4, 5]

def sqr(x): return x ** 2

list(map(sqr, items))

#Output: [1, 4, 9, 16, 25]

We passed in a user-defined function applied to each item in the list. map calls sqr on each list item and collects all the return values into a new list.

 

 

# Program to show the use of map function with multiple sequences

list(map(pow, [2, 3, 4], [10, 11, 12]))#Output: [1024, 177147, 16777216]

In the above example pow function takes 2 arguments, so we passed 2 sequences to map function.

Because map expects a function to be passed in, it also happens to be one of the places where lambda routinely appears:

 

# Program to show the use of map function with lambda

items = [1, 2, 3, 4, 5]

list(map((lambda x: x **2), items))

#Output: [1, 4, 9, 16, 25]

In the example above, the lambda function squares each item in the items list.

While we still use lambda as a aFunction, we can have a list of functions as aSequence:

 

# Program to show the use of map function with lambda and list of functions as sequences

def square(x):       return (x**2)def cube(x):       return (x**3)funcs = [square, cube]for r in range(5):   value = map(lambda x: x(r), funcs)   print value#Output: [0, 0]               [1, 1]               [4, 8]               [9, 27]               [16, 64]         

In the above example we get both square and cube of every value of r

Reduce

Syntax of reduce Function

reduce(aFunction, aSequence)

The reduce is in the functools in Python 3.0. The reduce function is used to apply a particular function passed in its argument to all of the list elementsmentioned in the sequence passed along. This function reduces a list to a single value by combining elements via a supplied function

# Program to show the use of reduce function

import functools L = [‘Testing ‘, ‘shows ‘, ‘the ‘, ‘presence’, ‘, ‘,’not ‘, ‘the ‘, ‘absence ‘, ‘of ‘, ‘bugs’]functools.reduce((lambda x,y:x+y), L)

#Output: ‘Testing shows the presence, not the absence of bugs

At each step, reduce passes the current product or division, along with the next item from the list, to the passed-in lambda function.

# Program to show same result without reduce function

L = [‘Testing ‘, ‘shows ‘, ‘the ‘, ‘presence’, ‘, ‘,’not ‘, ‘the ‘, ‘absence ‘, ‘of ‘, ‘bugs’]result = [ ]for x in L[1:]:               result = result + x print(result)#Output: ‘Testing shows the presence, not the absence of bugs >>>

 

The built-in reduce also allows an optional third argument placed before the items in the sequence to serve as a default result when the sequence is empty

Filter

Syntax of filter Function

filter(aFunction, aSequence)

The filter function in Python takes in a function and a list as arguments.The function is called with all the items in the list and a new list is returned which contains items for which the function evaluates to True.

# Program to show the use of filter function

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(filter(lambda x: (x%2 == 0), my_list))

print(new_list)

# Output: [4, 6, 8, 12]

 

Conclusion:

Python higher order functions are very powerful and make python the better programming language for functional programming.

Kalyan Sudhakar

Kaylan Sudhakar

Leave a Reply

(required)

(required)


ADVERTISEMENT

Gartner

WomeninTech