Get startedGet started for free

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

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

Good work with lambda expressions! In this lesson we'll cover three functions that are very powerful, especially, when combined with lambda expressions. These are map(), filter(), and reduce().

2. map()

Let's start with map().

3. map()

This function can take a bunch of iterable objects, like lists.

4. map()

and a mapping function that defines a rule which says how items with the same index will be mapped to a new object. Note that the amount of arguments in the mapping function equals the amount of considered Iterables.

5. map() with single Iterable

Let's consider the easiest mapping with a single Iterable. We want the square of each element in this list. First, we need to define our mapping function. Next, we pass it together with our list to the map() function. The output represents a map object. It is Iterable, meaning we can use it in a for loop,

6. map() with single Iterable

or within the list constructor.

7. map() with single Iterable

map is also an Iterator. It means we can retrieve consecutive elements by using the next() function.

8. map() with lambda expressions

So, here is our map() function. Recalling our previous lesson, you surely noticed that we can use a lambda expression instead of a normally defined function. This simplifies the code a lot!

9. map() with multiple Iterables

Now, let's consider multiple Iterables. Assume we have these two lists. We want to get a new list of the following form. In this case, our mapping will look like this. Here, elements with the same indices will be multiplied. Converting the map object to a list gives us the expected result.

10. filter()

The next useful function is filter().

11. filter()

The function takes an Iterable,

12. filter()

and a function that maps each item in the Iterable to a boolean value. The True indicates that the corresponding item is kept for further consideration.

13. filter() example

Let's have an example where we want to keep only positive numbers from the following list. First, we define our mapping function. Then, we pass it together with our list to the filter() function. Similar to map(), the output represents a filter object. This object is an Iterable. We can use it in a for loop,

14. filter() example

or within the list constructor.

15. filter() example

A filter object is also an Iterator. We can apply the next() function on it.

16. filter() with lambda expressions

And just as with the map() function, the filter() function often uses a lambda expression instead of a normally defined function in its call. Therefore, don't hesitate to use lambda expressions in filter() calls.

17. reduce()

So, here's the last useful function: the reduce() function from the functools module. As filter(), it takes an Iterable and a function having two arguments. reduce() summarizes a given Iterable into a new object having the same type as the Iterable content. How does it work? Let's see with our list.

18. reduce()

We start by applying the passed function on the first two items, which produces some output a, having the same type as the given items.

19. reduce()

We take this output and pass it together with the next item to our function again, which produces a new output.

20. reduce()

and so on

21. reduce()

until we get our final output d.

22. reduce() example

Let's have an example. Assume we want to find the smallest number in this list. First, we need to define the function with the pair of arguments. Then, we pass it together with our list to the reduce() function. Compared to map() or filter(), it simply returns a value. What happens here? The first pair gives 4 as the minimum. Then, 4 is compared to 5. 4 is still the minimum. Then, 4 is compared to 1. 1 is the new minimum. Finally, 1 is compared to 9. 1 is the final result.

23. reduce() with lambda expressions

We can rewrite reduce() with a lambda expression, which decreases the amount of code.

24. Let's practice!

That's it on map(), filter(), and reduce(). Let's practice now!