Map(), Filter() and Reduce() - Higher Order Functions
Basically, all these high-order functions apply a function to an iterable.
from functools import reduce
def square(n): return n**2
def greaterThan(n): return n>2
def addAll(n, m): return n+m
mylist=[1,2,3,4,5]
print(list(map(square, mylist)))
print(list(filter(greaterThan, mylist)))
print(reduce(addAll, mylist))
Comments
Post a Comment
Write something to CodeWithAbdur!