What are the functions map(), filter(), reduce()?

Esercitarsi con le domande di colloquio di coding in Python

Kirill Smirnov

Data Science Consultant, Altran

map()

map(                                                )
Esercitarsi con le domande di colloquio di coding in Python

map()

map(                       Iterable1, Iterable2, ...)

Iterables: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

Esercitarsi con le domande di colloquio di coding in Python

map()

map(function(x1, x2, ...), Iterable1, Iterable2, ...)

Iterables: [1, 2, 3, 4, 5], [10, 20, 30, 40, 50], ...

1, 10, ... $\rightarrow$ function(1, 10, ...) $\rightarrow$ new object

2, 20, ... $\rightarrow$ function(2, 20, ...) $\rightarrow$ new object

3, 30, ... $\rightarrow$ function(3, 30, ...) $\rightarrow$ new object

4, 40, ... $\rightarrow$ function(4, 40, ...) $\rightarrow$ new object

5, 50, ... $\rightarrow$ function(5, 50, ...) $\rightarrow$ new object

Esercitarsi con le domande di colloquio di coding in Python

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares is iterable

for square in squares:
    print(square)
1
4
9
16
25
Esercitarsi con le domande di colloquio di coding in Python

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares is Iterable

list(squares)
[1, 4, 9, 16, 25]
Esercitarsi con le domande di colloquio di coding in Python

map() with single Iterable

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
print(squares)
<map object at 0x7fdbe4ab3da0>

squares is Iterator

next(squares)
1
next(squares)
4
Esercitarsi con le domande di colloquio di coding in Python

map() with lambda expressions

nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

def squared(x):
    return x**2
squares = map(squared, nums)
list(squares)
[1, 4, 9, 16, 25]
nums = [1, 2, 3, 4, 5]

The task is to get [1, 4, 9, 16, 25]

squares = map(lambda x: x**2, nums)
list(squares)
[1, 4, 9, 16, 25]
Esercitarsi con le domande di colloquio di coding in Python

map() with multiple Iterables

nums1 = [1, 2, 3, 4, 5]
nums2 = [10, 20, 30, 40, 50]

The task is to get: [1*10, 2*20, 3*30, 4*40, 5*50] = [10, 40, 90, 160, 250]

mult = map(lambda x, y: x*y, nums1, nums2)
list(mult)
[10, 40, 90, 160, 250]
Esercitarsi con le domande di colloquio di coding in Python

filter()

filter(                     )
Esercitarsi con le domande di colloquio di coding in Python

filter()

filter(             Iterable)

Iterable: [1, 2, 3, 4, 5]

Esercitarsi con le domande di colloquio di coding in Python

filter()

filter(function(x), Iterable)

Iterable: [1, 2, 3, 4, 5]

1 $\rightarrow$ function(1) $\rightarrow$ True $\rightarrow$ 1 is kept

2 $\rightarrow$ function(2) $\rightarrow$ False $\rightarrow$ 2 is rejected

3 $\rightarrow$ function(3) $\rightarrow$ True $\rightarrow$ 3 is kept

4 $\rightarrow$ function(4) $\rightarrow$ False $\rightarrow$ 4 is rejected

5 $\rightarrow$ function(5) $\rightarrow$ True $\rightarrow$ 5 is kept

Esercitarsi con le domande di colloquio di coding in Python

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj is Iterable

for item in fobj:
    print(item)
1
2
3
Esercitarsi con le domande di colloquio di coding in Python

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj is Iterable

list(fobj)
[1, 2, 3]
Esercitarsi con le domande di colloquio di coding in Python

filter() example

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
print(fobj)
<filter object at 0x7f196d378d68>

fobj is Iterator

next(fobj)
1
next(fobj)
4
Esercitarsi con le domande di colloquio di coding in Python

filter() with lambda expressions

nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

def positive(x):
    return x > 0
fobj = filter(positive, nums)
list(fobj)
[1, 2, 3]
nums = [-3, -2, -1, 0, 1, 2, 3]

The task is to get: [1, 2, 3]

fobj = filter(lambda x: x > 0, nums)
list(fobj)
[1, 2, 3]
Esercitarsi con le domande di colloquio di coding in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ new object of the same type as the content

The representation of the given list

Esercitarsi con le domande di colloquio di coding in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ new object of the same type as the content

Combining the first two elements in the list

Esercitarsi con le domande di colloquio di coding in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ new object of the same type as the content

Combining the output with the third element in the list

Esercitarsi con le domande di colloquio di coding in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ new object of the same type as the content

Combining the output with the fourth element in the list

Esercitarsi con le domande di colloquio di coding in Python

reduce()

from functools import reduce

reduce(function(x, y), Iterable)

Iterable: [1, 2, 3, 4, 5]

[1, 2, 3, 4, 5] $\rightarrow$ new object of the same type as the content

Combining the output with the fifth element in the list

Esercitarsi con le domande di colloquio di coding in Python

reduce() example

nums = [8, 4, 5, 1, 9]

The task is to get: 1 - minimum

def smallest(x, y):
    if x < y:
        return x
    else:
        return y
reduce(smallest, nums)
1

smallest(8, 4) $\rightarrow$ 4

smallest(4, 5) $\rightarrow$ 4

smallest(4, 1) $\rightarrow$ 1

smallest(1, 9) $\rightarrow$ 1 - final result

Esercitarsi con le domande di colloquio di coding in Python

reduce() with lambda expressions

nums = [8, 1, 4, 2, 9]

The task is to get: 1 - minimum

def smallest(x, y):
    if x < y:
        return x
    else:
        return y
reduce(smallest, nums)
1
nums = [8, 1, 4, 2, 9]

The task is to get: 1 - minimum

reduce(lambda x, y: x if x < y else y, nums)
1
Esercitarsi con le domande di colloquio di coding in Python

Let's practice!

Esercitarsi con le domande di colloquio di coding in Python

Preparing Video For Download...