Python map-reduce-filter

From Bitnami MediaWiki
Revision as of 19:02, 11 September 2022 by User (talk | contribs)
Jump to navigation Jump to search

Map, Reduce and Filter

  • Map: applies a function to each value of an iterable, with basic syntax: map(function, iterable).

<syntax

 
def square(x): 
    return x*x 

mynums = [17, 10,  3,  1,  5, 14, 15, 10,  6, 12] 
result = map(square, mynums)
# or
result = map(lambda x: x*x, mynums)
# output 
list(result)
[289, 100, 9, 1, 25, 196, 225, 100, 36, 144]