How do map, reduce and filter functions work in Python?


In this article, we will show you the

Python's map(), filter(), and reduce() functions add a touch of functional programming to the language. All three of these are convenience functions that can be replaced with List Comprehensions or loops but offer a more elegant and concise solution to some problems.

map(), filter(), and reduce() all work in the same way. These functions accept a function and a sequence of elements and return the result of applying the received function to each element in the sequence.

map() function

Like reduce(), the map() function allows you to iterate over each item in an iterable. Map(), on the other hand, operates independently on each item rather than producing a single result.

Finally, the map() function can be used to perform mathematical operations on two or more lists. It can even be used to manipulate any type of array.

The map() function’s time complexity= O (n)

Syntax

map(function, iterable)

Parameters

  • function − The function to be used in the code.

  • iterable − This is the value that is iterated in the code.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a function with the name multiplyNumbers that returns the multiplication result of the number passed to it.

  • Return the given number multiplied by 3, inside the function.

  • Use the map() function for applying the multiplyNumbers() function for each element of the list by passing the function name, and list as arguments to it.

  • Print the resultant list items after multiplying them with 3.

Code

# creating a function that returns multiplication result def multiplyNumbers(givenNumbers): # returning number after multiplying with 3 return givenNumbers*3 # map() function applies the multiplyNumbers function # for each element of the list givenNumbers = map(multiplyNumbers, [1, 3, 5, 2, 6, 10]) # Printing the resultant list items print("Multiplying list elements with 3:") for element in givenNumbers: print(element)

Output

On executing, the above program will generate the following output −

Multiplying list elements with 3:
3
9
15
6
18
30

filter() function

The filter() function creates a new iterator that filters elements from a previously created one (like a list, tuple, or dictionary).

The filter() function checks whether or not the given condition is present in the sequence and then prints the result.

The filter() function’s time complexity= O (n)

Syntax

filter(function, iterable)

Parameters

  • function − The function to be used in the code.

  • iterable − This is the value that is iterated in the code.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Create a function with the name votingAge that returns the eligibility ages for voting from the list.

  • Use the if conditional statement to check whether the number passed to the function is greater than or equal to 18.

  • If the above statement is true Return the number.

  • Create a variable to store the input list.

  • Use the filter() function by passing the function name, and input list as arguments to it to filter the ages greater than or equal to 18 from the list. Here it applies the votingAge() function to every element of the list and the result stores only the values of the list that are returned by the votingAge() function(Here votingAge() function returns the number if it is greater than 18).

  • Print the filter object

  • Use the list() function(returns a list of an iteratable), to convert the above filter object into a list and print it.

Example

# creating a function that returns the eligibility ages for voting from the list def votingAge(givenNumumber): # checking whether the number is greater than or equal to 18 if givenNumumber>=18: # returning number return givenNumumber # input list inputList = [3, 20, 18, 6, 14, 25, 19] # Getting only values of above list which are greater than or equal to 18 result_filterObj = filter(votingAge, inputList) # printing the filter object print(result_filterObj) # converting into a list print("Eligibility ages for voting from the input list:", list(result_filterObj))

Output

On executing, the above program will generate the following output –

<filter object at 0x7fcd3ad14280>
Eligibility ages for voting from the input list: [20, 18, 25, 19]

reduce()

In Python, the reduce() function iterates through each item in a list or other iterable data type, returning a single value. It's in the functools library. This is more efficient than looping.

Syntax

reduce(function, iterable)

Parameters

  • function − The function to be used in the code.

  • iterable − This is the value that is iterated in the code.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –

  • Use the import keyword to import the reduce() function from functools module

  • Create a function with the name addNumbers() that returns the sum of all list items.

  • Create a variable to store the input list

  • Use the reduce() function, bypassing the addNumbers() function, and input list as arguments to get the sum of all list items.

Example

# importing reduce() function from functools module from functools import reduce # function that returns the sum of all list items def addNumbers(x, y): return x+y # input list inputList = [12, 4, 10, 15, 6, 5] # Print the sum of the list items using reduce() function print("The sum of all list items:") print(reduce(addNumbers, inputList))

Output

The sum of all list items:
52

When we pass the addNumbers() function and the input list as arguments to the reduce() function, it will take two elements of the list and sum them to make one element, then take another list element and sum it again to make one element, and so on until it sums all of the list's elements and returns a single value.

Conclusion

This article covered the map(), reduce(), and filter() functions, as well as their syntax and examples.

Updated on: 15-Sep-2022

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements