Map, Filter and Reduce
All those three functions has similar syntax as shown below.
map(function_to_apply, list_of_inputs)
map
numbers = [1, 2, 3, 4]
result = map(lambda x: x + x, numbers)
>>> list(result)
[2, 4, 6, 8]
filter
number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
>>> less_than_zero
[-5, -4, -3, -2, -1]
reduce
Example 1:
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
>>> product
24
Example 2:
from functools import reduce
func = lambda s, t: s.replace(*t)
>>> reduce(func, [('a','b'), ('c', 'd')], 'abc')
'bbd'
It’s definition roughly equivalent to:
def reduce(function, iterable, initializer=None):
it = iter(iterable)
if initializer is None:
value = next(it)
else:
value = initializer
for element in it:
value = function(value, element)
return value