Learn everything about Analytics

Home » Python most powerful functions: map(), filter(), and reduce() in 5 minutes

Python most powerful functions: map(), filter(), and reduce() in 5 minutes

This article was published as a part of the Data Science Blogathon

Overview

Python includes a number of predefined built-in functions that can be utilized by the end-user by simply calling them. These functions not only make programmers’ jobs easier, but they help establish a common coding environment. In this tutorial, you’ll learn about three of Python’s most powerful functions: map(), filter(), and reduce().

Functional programming’s three pillars are map, filter, and reduce functions. While Python isn’t exactly a functional programming language, it does have a lot of features that make it so. The map, filter, and reduce functions in Python are discussed in this article, as well as how they correspond to functional programming principles.

Functional Programming

A programming paradigm that uses functions to define computation is known as functional programming. The concept of an unchangeable state is one of functional programming’s key defining traits.

Computation is done through statements in imperative programming, which is arguably the most prevalent programming paradigm you’re already familiar with. These are commands that affect the value of a variable, and thus the state of the computation after they are executed. A for loop, for example, can execute a statement repeatedly, altering the value of a variable each time, as shown below:

counter = 0
for i in range(10):
    counter += 1

Each time the value of the counter is increased by one in each iteration of the loop, the state of the calculation changes, bringing it closer to the end state.

Before we go into examples of ‘map()’, ‘filter()’, and ‘reduce()’ functions in Python, we’ll need to go over another concept: higher-order functions.

Higher-Order Functions

In functional programming, higher-order functions are our primary tool for defining computation. These are functions that take a function as a parameter and return a function as the result. Reduce(), map(), and filter() are three of Python’s most useful higher-order functions. They can be used to do complex operations when paired with simpler functions.

A higher-order function is demonstrated in the code sample below. print greeting() takes two arguments: a function f and a name n, and returns the result of calling f. (n).

def greet(name):
    return "Hello, {}!".format(name)

def print_greeting(f, n):
    print(f(n))

Anonymous Functions

We saw an example of a function that takes another function as an argument in the previous section. The functions map(), filter(), and reduce() all do the same thing: They each take a function and a list of elements, and then return the result of applying the function to each element in the list.

As previously stated, Python has built-in functions like map(), filter(), and reduce(). Python’s functional programming feature is enabled through these functions. The only factors that determine the output in functional programming are the inputs passed in. These functions can accept any other function as a parameter and can also be passed as parameters to other functions. Let’s look at each of these functions in more detail now.

we have three main functions:

  1. map()
  2. filter()
  3. reduce()

1). The map() function:

The map() function is a higher-order function. As previously stated, this function accepts another function and a sequence of ‘iterables’ as parameters and provides output after applying the function to each iterable in the sequence. It has the following syntax:

SYNTAX: map(function, iterables)

The function is used to define an expression which is then applied to the ‘iterables’. User-defined functions and lambda functions can both be sent to the map function.

User-defined functions can be sent to the map() method. The user or programmer is the only one who can change the parameters of these functions.

EXAMPLE

def function(a):
    return a*a
x = map(function, (1,2,3,4))  #x is the map object
print(x)
print(set(x))

OUTPUT


{16, 1, 4, 9}

x is a map object, as you can see. The map function is displayed next, which takes “function()” as a parameter and then applies “a * a” to all ‘iterables’. As a result, all iterables’ values are multiplied by themselves before being returned.

Let’s look at how lambda functions can be used within the map() method.

Lambda within map() functions:

Functions with no name are known as lambda functions. These functions are frequently used as input to other functions. Let’s try to integrate Lambda functions into the map() function.

EXAMPLE

tup= (5, 7, 22, 97, 54, 62, 77, 23, 73, 61)
newtuple = tuple(map(lambda x: x+3 , tup)) 
print(newtuple)

OUTPUT

(8, 10, 25, 100, 57, 65, 80, 26, 76, 64)

The filter() function:

The filter() function is used to generate an output list of values that return true when the function is called. It has the following syntax:

SYNTAX: filter (function, iterables)

This function like map(), can take user-defined functions and lambda functions as parameters.

EXAMPLE

def func(x):
    if x>=3:
        return x
y = filter(func, (1,2,3,4))  
print(y)
print(list(y))

OUTPUT

[3, 4]

As you can see, y is the filter object, and the list is a collection of true values for the condition (x>=3).

Lambda within filter() functions:

The condition to be checked is defined by the lambda function that is provided as an argument.

EXAMPLE

y = filter(lambda x: (x>=3), (1,2,3,4))
print(list(y))

OUTPUT

[3, 4]

The reduce() function:

The reduce() function applies a provided function to ‘iterables’ and returns a single value, as the name implies.

reduce function | python most powerful functions

SYNTAX: reduce(function, iterables)

The function specifies which expression should be applied to the ‘iterables’ in this case. The function tools module must be used to import this function.

EXAMPLE

from functools import reduce
reduce(lambda a,b: a+b,[23,21,45,98])

OUTPUT

187

The reduce function in the preceding example adds each iterable in the list one by one and returns a single result.

The Python functions map(), filter(), and reduce() can all be used together.

So, This article on map(), filter(), and reduce functions in Python. I hope you have understood everything clearly. Make sure you practice on map(), filter(), and reduce() because this function is very useful when we are doing data analysis using python.

EndNote

Thank you for reading!
I hope you enjoyed the article and increased your knowledge.
Please feel free to contact me on Email
Something not mentioned or want to share your thoughts? Feel free to comment below And I’ll get back to you.

About the Author

Hardikkumar M. Dhaduk
Data Analyst | Digital Data Analysis Specialist | Data Science Learner
Connect with me on Linkedin
Connect with me on Github

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.
You can also read this article on our Mobile APP Get it on Google Play