How to use map, filter and reduce?

When working on Python programming you inevitably come across situations where you have to do some data manipulation. In most cases, you use control statements to get the desired result, but these control statements can quickly become a bit messy and large. Functional techniques can help you write more declarative code that is easier to understand at a glance, refactor, and test. Most of the times it can be much easier to use the map , filter or reduce methods. The rule of thumb you use to determine which method you should use is as follows:
  1. If you already have a list of values and you want to do the exact same operation on each of the elements in the array and return the same amount of items in the list, in these type of situations it is better to use the map method.

  2. If you already have list of values but you only want to have items in the array that match certain criteria, in these type of situations it is better to use the filter method.

  3. If you already have list of values, but you want to use the values in that list to create something completely new, in these type of situations it is better to use the reduce method.

Map

Map operation takes a mapping function and a vector of data as arguments and returns a new vector, which is the result of applying the mapping function on each element of the vector independently. The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) and so on. Syntax
map(function_to_apply, list_of_inputs)
  1. function_to_apply - map() passes each item of the iterable to this function.

  2. list_of_inputs - iterable which is to be mapped
example
def square(n): return n*n map_inputs = (1, 2, 3, 4) map_ret = map(square, map_inputs) print(map_ret) list_square = list(map_ret) print(list_square)
output
< map object at 0x000000000293CF60 > [1, 4, 9, 16]

Most of the times map function use lambdas.

map_inputs = (1, 2, 3, 4) map_ret = map(lambda n: n**2, map_inputs) print(map_ret) list_square = list(map_ret) print(list_square)
output
< map object at 0x00000000028AC4E0 > [1, 4, 9, 16]

or

map_inputs = (1, 2, 3, 4) print(list(map(lambda n: n**2, map_inputs)))
output
[1, 4, 9, 16]

Filter

The filter function operates on a list and returns a subset of that list after applying the filtering rule. example
in_list = [98,99,100,101,102] out_list = filter(lambda x: x > 100, in_list) print(list(out_list))
output
[101, 102]

Reduce

The reduce function will transform a given list into a single value by applying a given function continuously to all the elements. It basically keeps operating on pairs of elements until there are no more elements left.

The following example shows how to find the product of given numbers.

result = 1 in_num = [1, 2, 3, 4,5] for num in in_num: result = result * num print(result)
output
120
Using Reduce method:
from functools import reduce result = reduce((lambda x, y: x * y), [1, 2, 3, 4,5]) print(result)
output
120