Simple MapReduce in Python
Posted July 27, 2022 by Rohith ‐ 2 min read
A simple mapreduce operation in python.
This blog is intended to simple operations on data using python on a single machine. Python considers functions as first class citizens. And it provides methods like map()
, reduce()
, filter()
and many more inbuilt methods to manipulate the data.
map() Function In Python
map
function takes a function
(usually lambda
) and iterable object like (list
, tuple
, etc.) and applies the function to each element int the iterable object. map
return iterator
Example:
sample_tuple = ("PYTHON", "PROGRAMMING", "LANGUAGE", "IS", "MODERN", "LANGUAGE")
lower_sample_tuple = map(lambda e: e.lower(), sample_tuple)
print(list(lower_sample_tuple))
Output:
['python', 'programming', 'language', 'is', 'modern', 'language']
reduce() Function In Python
The reduce()
function is not a built-in function, it has to be imported from functools
. reduce()
function works similar to map, but the lambda function takes two arguments and returns a reduced value by reduce operation in reduce function.
reduce()
returns the list.
Example:
from functools import reduce
sample_tuple = ("PYTHON", "PROGRAMMING", "LANGUAGE", "IS", "MODERN", "LANGUAGE")
reduce_sample_tuple = reduce(lambda e1, e2: f'{e1}, {e2}', sample_tuple)
print(reduce_sample_tuple)
Output
'PYTHON, PROGRAMMING, LANGUAGE, IS, MODERN, LANGUAGE'
MapReduce in python
map
and reduce
operations combined can be a powerful way to manipulate the data.
Example: 1 Extending examples from map
and reduce
sample_tuple = ("PYTHON", "PROGRAMMING", "LANGUAGE", "IS", "MODERN", "LANGUAGE")
lower_sample_tuple = map(lambda e: e.lower(), sample_tuple)
reduce_sample_tuple = reduce(lambda e1, e2: f'{e1}, {e2}', lower_sample_tuple)
print(reduce_sample_tuple)
Output:
'python, programming, language, is, modern, language'