0% found this document useful (0 votes)
74 views21 pages

Functional Approach in Python

The document discusses functional programming concepts in Python including lambda functions, map(), filter(), and reduce(). Lambda functions allow creating small anonymous functions. Map() applies a function to each item in an iterable and returns the results. Filter() applies a function to each item and returns elements where the function returns True. Reduce() applies a rolling computation to sequential pairs in a list.

Uploaded by

Manan kansara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views21 pages

Functional Approach in Python

The document discusses functional programming concepts in Python including lambda functions, map(), filter(), and reduce(). Lambda functions allow creating small anonymous functions. Map() applies a function to each item in an iterable and returns the results. Filter() applies a function to each item and returns elements where the function returns True. Reduce() applies a rolling computation to sequential pairs in a list.

Uploaded by

Manan kansara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Functional

Approach in
Python
 Functional programming is all about
expressions. We may say that the
Functional programming is an expression
oriented programming.
 Expression oriented functions of Python
provides are:
lambda
map(aFunction, aSequence)
filter(aFunction, aSequence)
reduce(aFunction, aSequence)
list comprehension
lambda function
 A way to create small anonymous
functions, i.e. functions without a name.
 Mainly used in combination with the
functions filter(), map() and reduce().
 Syntax:
lambda argument_list: expression
 Ex:
def add(x,y):
return x+y

# call the function


print(add(2,3)) # output : 5

#Functional approach
add = lambda x, y : x + y
print(add(2,3))

Output:
5
 Ex:
f = lambda x: x<100
print(f(90))

Output:
True

 Ex:
f = lambda x: 'big' if x > 100 else 'small'
print(f(100))

Output:
small
Map() function
 The map(aFunction, aSequence) function
applies a passed-in function to each item
in an iterable object and returns a list
containing all the function call results.
 Ex:
items = [1, 2, 3, 4, 5]
def sqr(x):
return x ** 2
print(list(map(sqr, items)))

Output:
[1, 4, 9, 16, 25]
 Ex:
#python 3
items = [1, 2, 3, 4, 5]
print(list(map((lambda x: x **2),
items)))

#python 2
items = [1, 2, 3, 4, 5]
print(map((lambda x: x **2), items))
 While we still use lamda as a aFunction, we can
have a list of functions as aSequence.
 Ex:
def square(x):
return (x**2)
def cube(x):
return (x**3)
funcs = [square, cube]
for r in range(5):
value = list(map(lambda x: x(r), funcs))
print(value)
 Output:
[0, 0]
[1, 1]
[4, 8]
[9, 27]
[16, 64]
 Multiple iterables to the map function
 Ex:
a = [1, 2, 3]
b = [10, 20, 30]
print(list(map(lambda x, y: x + y, a, b)))

Output:
[11,22,33]
filter() function
 The filter(aFunction, aSequence) function
applies a passed-in function to each item
in an iterable object and extracts each
element in the sequence for which the
function returns True.
 Ex:-
list(range(-5,5))
# [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
print(list(filter((lambda x: x < 0), range(-5,5))))
print(list(map((lambda x: x < 0), range(-5,5))))

Output:-
[-5, -4, -3, -2, -1]
[True, True, True, True, True, False, False, False,
False, False]
Finding intersections of two lists
 Ex:
a = [1,2,3,5,7,9]
b = [2,3,5,6,7,8]

print(filter(lambda x: x in a, b))

Output:-
[2, 3, 5, 7]
 Ex:
fib = [0,1,1,2,3,5,8,13,21,34,55]
result = filter(lambda x: x % 2, fib)
print(result)
result = filter(lambda x: x % 2 == 0, fib)
print(result)

Output:
[1, 1, 3, 5, 13, 21, 55]
[0, 2, 8, 34]
How filter() method works
without the filter function?
 Ex:
# random list
randomList = [1, 'a', 0, False, True, '0']
filteredList = filter(None, randomList)
print('The filtered elements are:')
for element in filteredList:
print(element)
 Output:
The filtered elements are:
1
a
True
0
reduce() function
 reduce(aFunction, aSequence)
 It applies a rolling computation to sequential pairs
of values in a list.
 Ex:
from functools import reduce #python3
print(reduce( (lambda x, y: x * y), [1, 2, 3, 4] ))

Output:
24
 Ex:
from functools import reduce
#python3
print(reduce( (lambda x, y: x / y), [1, 2,
3, 4] ))

Output:
0.041666666666666664
 Determining the maximum of a list of
numerical values by using reduce:

from functools import reduce


f = lambda a,b: a if (a > b) else b
print(reduce(f, [47,11,42,102,13]))

Output:
102
 Calculating the sum of the numbers from
1 to 100:

from functools import reduce


print(reduce(lambda x, y: x+y,
range(1,101)))

Output:
5050

You might also like