Lambda, Map, Filter, Reduce
Hi! In this post, some useful built in functions in Python are introduced. They might not be intuitively understandable in the beginning but once you get the ideas, they are super useful! So let’s get started.
Lambda function
- No limit on the parameters
- Useful for changing the function or the value of the variables
- key attributes on list.sort() can be implemented as well
(check the example below)- lambda parameters [ … ] : expression
Let’s look at this python function example.
def sum(x, y):
return x + y
result = sum(2, 3)
print(result)
5
Now, use lambda function to carry out the same result.
result1 = (lambda x, y : x + y)(2, 3)
print(result1)
5
func = lambda x, y : x + y
print(func(2, 3))
5
func1 = lambda x, y = 10 : x + y
print(func1(5))
print(func1(5, 6))
15 11
# mutable variable
func2 = lambda a, *b, **c : print(a, b, c)
func2(1, 2, 3, n = 4, m = 5)
1 (2, 3) {'n': 4, 'm': 5}
Lambda function with list.sort()
lst = [1, 5, 3, 9, 8, 4, 2]
lst.sort()
lst
[1, 2, 3, 4, 5, 8, 9]
lst = [1, 5, 3, 9, 8, 4, 2]
lst.sort(key = lambda a : a)
lst
[1, 2, 3, 4, 5, 8, 9]
lst1 = [('john', 20, 100), ('sally', 10, 50), ('adriene', 30, 60)]
lst1.sort(key = lambda a : a[2])
lst1
[('sally', 10, 50),('adriene', 30, 60),('john', 20, 100)]
# by using lower(), it changes all the alphabet into small letters then sort
lst2 = [('Aa'), ('DEF'), ('cEf')]
lst2.sort(key = lambda a : a.lower())
lst2
['Aa', 'cEf', 'DEF']
Map function
- applies the (given) function on the (given) list and returns the result as another (iterable) list
- map(func, iterable)
Let’s see how this python function example can be simplified using map function.
def calc(x):
return x*2
b = []
for n in [1, 2, 3, 4]:
a = calc(n)
print(a, end = ' ')
b.append(a)
print(b)
print(list(map(calc, [1, 2, 3, 4])))
[2, 4, 6, 8]
list(map(lambda x : x**2, range(5))
[0, 1, 4, 9, 16]
list(map(lambda x : x**2, [2, 2, 4, 5])
[4, 4, 16, 25]
Reduce function
- Mainly used for counting
- Need to import funtools module in order to use it
from functools import reduce
reduce(lambda x, y : x + y, [1, 2, 3, 4, 5])
15
# when there is only one parameter, it gives you an error
# TypeError: <lambda>() takes 1 positional argument but 2 were given
reduce(lambda x : x**2, [2, 2, 4, 5])
The example above could be extended as python function like this:
def userReduce():
data = [1, 2, 3, 4, 5]
v = 0
for i in data:
v = v + i
print(v)
userReduce()
15
Filter function
- It subtracts few parts of the data (it literally filters the data)
- filter(func, list)
list(filter(lambda x : x < 5, range(10)))
[0, 1, 2, 3, 4]