Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Combine filter() and map()

00:00 In this lesson, you’ll be learning what the map() function does and how you can combine it with filter(). The map() function is a Python built-in function, which means you can use it right away without the need to import any libraries to use it.

00:17 The map() function is used when you need to apply a function to each element of an iterable object and generate a new iterable with the results.

00:27 If this sounds a bit abstract, don’t worry. You’ll be using map() shortly.

00:34 The map() function takes in two arguments, function and iterable. In the map() function, you pass in a function that will be applied to each element of the iterable.

00:46 And as a reminder, an iterable is any object that can be looped over, such as a list, a tuple, or a string. In the map() function, you pass an iterable object that will have the function applied to each of its elements.

01:02 Let’s explore an example where you’ll use map() to square all the numbers inside of a list. In this example, you’ll use map() to square all the numbers inside of a list.

01:15 numbers = [1, 2, 3, 4, 5].

01:22 Now what you are trying to do is to square all these numbers. To do that, you’ll need a function. Let’s name this function square(). It should take a number as an input, and it should return the square of this number. Thankfully, Python has a built-in operator for exponentiation, which is **, so number ** 2 is number to the power of 2.

01:49 Then it’s time to use map() to apply the square() function you just created to each number in the numbers list. As a reminder, map() returns an iterator object, so you’ll need to call the list() function on the result to be able to see it.

02:05 Let’s store the results in a variable named squares. list(map()). As its function argument, let’s put in square—of course, here you need a function object, not a function call, so no parentheses—and numbers as its iterable.

02:24 Let’s see the results.

02:29 You get 1, 4, 9, 16, and 25, which are 1, 2, 3, 4, and 5 squared. You did it.

02:38 What happened here is that the map() function applied the square() function to every single number in numbers. For example, it put in 2 from the numbers list inside of the square() function, and 2 to the power of 2 is 4, so the square() function returned 4, and map() stored it.

02:55 This happened to every single number in the numbers list.

03:03 Great, now you know how the map() function works and how you can use it to square every number of a list. It’s time to explore how you can combine map() with filter().

03:14 Your goal is extracting the even numbers of an iterable using filter() and then squaring them all using map().

03:24 Let’s start by creating a list of integers and naming it numbers. numbers = [1, 3, 10, 45, 6, 50].

03:37 Next, let’s extract the even numbers of this list using filter(). You’ve done this a couple of times by now. For the predicate function, let’s create a function named is_even, which checks whether a number is even or not by computing is remainder when you divide it by two.

03:55 def is_even(), number as an input, return number % 2 == 0. So is_even() will return True if number % 2 is 0.

04:10 Or, in other words, when you divide number by 2, it doesn’t have any remainders.

04:16 Now it’s time to use the filter() function with is_even as its predicate function and numbers as its iterable argument and store its results in a list named even_numbers.

04:28 even_numbers = list(filter()), is_even as the predicate function and numbers as the iterable. Let’s see the results. 10, 6, and 50, so it means you filtered out the odd numbers, 1, 3, and 45. It worked. Now that you have your even numbers, it’s time for the interesting part: combining all of this with map().

04:57 In the first example that you used map() to square all the numbers inside of a list, you created a function named square() to take in a number and then return the square of it. Here, let’s try a lambda function as map’s function argument that does the same, and make sure you’re converting the results into a list.

05:18 list(map). As map’s function argument, let’s create a lambda function that takes in a number and then squares it. lambda keyword, n as an input and for the return statement, n ** 2, or n the power of 2.

05:36 And as map’s iterable argument, let’s put in even_numbers.

05:41 What you expect to happen here is that all the even numbers—10, 6, and 50—to be squared. So you expect 100, 36, and 2500.

05:54 Let’s see if it worked. And it did. The result is a list: 100, which is 10 squared; 36, which is the square of 6; and 2500, which is the square of 50.

06:09 So far, you’ve used filter() to filter out the odd numbers, which you stored in a variable named even_numbers, and then you used map() to apply a lambda function to each number in even_numbers and squared them all, but this looks a bit long, right?

06:26 How do you think you can make this code shorter? Since the results of filter() is an iterator itself, you can just directly use it as map’s iterable argument instead of even_numbers.

06:38 Let’s do that right now. list(map()). As the function argument for map(), let’s reuse the same lambda function to square each input number.

06:50 Now, as the iterable argument for map(), let’s just directly use filter(). filter(), is_even as the predicate function, and numbers as the iterable you’re filtering odd numbers from. Everything stayed the same, but instead of creating a new list, like even_numbers, you’re directly using filter’s results as map’s iterator argument.

07:14 Let’s see the results. It remained the same: 100, 36, and 2500, which are 10, 6, and 50 squared, just like you wanted, but this time you did it with a one-liner.

07:27 What’s happening here is that the filter() function filters out the odd numbers of numbers and keeps the even numbers 10, 6, and 50.

07:37 Then map() is applying the lambda function on every number in the iterator that’s made by filter, so it takes 10, squares it, takes 6, squares it, and then takes 50 and squares it.

07:50 That’s why you got 100, 36, and 2500.

07:55 Congrats, you just learned how to combine map() and filter()!

Become a Member to join the conversation.