✌Higher-order functions in Python🤩
Higher-Order Functions:
A function is called Higher-Order Function if it contains other functions as a parameter or returns a function as an output. There are three built-in higher-order functions namely,
- Map
- Filter
- Reduce

map()
The map()
function iterates through all items in the given iterable and executes the function
we passed as an argument on each of them.
def sq(*a):
return a**2
a=[1,2,3,4,5]
print(map(sq,a))[1,4,9,16,25]
reduce()
Reduce doesn’t create a new list with that list we passed instead it returns a single value.
def sum(a,b):
return a+b
a=[1,2,3,4,5]
print(map(sq,a))15
filter()
It filters the array and returns only the elements of that array which passed that given condition.
def even(a):
return a%2==0
a=[1,2,3,4,5]
print(map(even,a))[2,4]
Comments
Post a Comment