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

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes