Combine filter() and map()
In this lesson, you’ll be learning what the
map() function does and how you can combine it with
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.
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.
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
number ** 2 is
number to the power of
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.
Let’s store the results in a variable named
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.
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
4, so the
square() function returned
map() stored it.
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.
even_numbers = list(filter()),
is_even as the predicate function and
numbers as the iterable. Let’s see the results.
50, so it means you filtered out the odd numbers,
45. It worked. Now that you have your even numbers, it’s time for the interesting part: combining all of this with
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.
list(map). As map’s function argument, let’s create a lambda function that takes in a number and then squares it.
n as an input and for the
n ** 2, or
n the power of
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?
Now, as the iterable argument for
map(), let’s just directly use
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.
Become a Member to join the conversation.