Movatterモバイル変換


[0]ホーム

URL:


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:00In this lesson,you’ll be learning what themap() function does and how you can combine it withfilter().Themap() function is a Python built-in function,which means you can use it right away without the need to import any librariesto use it.

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

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

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

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

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

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

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

01:49Then it’s time to usemap() to apply thesquare() function you just created to eachnumber in thenumbers list. As a reminder,map() returns an iterator object,so you’ll need to call thelist() function on the result to be able to see it.

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

02:24Let’s see the results.

02:29You get1,4,9,16, and25,which are1,2,3,4, and5 squared. You did it.

02:38What happened here is that themap() function applied thesquare() function to everysingle number innumbers. For example,it put in2 from thenumbers list inside of thesquare() function, and2to the power of2 is4,so thesquare() function returned4, andmap() stored it.

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

03:03Great,now you know how themap() function works and how you can use it to square everynumber of a list. It’s time to explore how you can combinemap() withfilter().

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

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

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

03:55defis_even(),number as an input,returnnumber % 2 == 0.Sois_even() will returnTrue ifnumber % 2 is0.

04:10Or, in other words, when you dividenumber by2,it doesn’t have any remainders.

04:16Now it’s time to use thefilter() function withis_even as its predicate functionandnumbers as its iterable argument and store its results in a listnamedeven_numbers.

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

04:57In the first example that you usedmap() to square all the numbers inside of alist,you created a function namedsquare() to take in a number and then return thesquare 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:18list(map). As map’s function argument,let’s create a lambda function that takes in a number and then squares it.lambdakeyword,n as an input and for thereturn statement,n ** 2, orn the power of2.

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

05:41What you expect to happen here is that all the even numbers—10,6, and50—to be squared.So you expect100,36, and2500.

05:54Let’s see if it worked. And it did.The result is a list:100, which is10 squared;36,which is the square of6; and2500, which is the square of50.

06:09So far, you’ve usedfilter() to filter out the odd numbers, which you stored in avariable namedeven_numbers,and then you usedmap() to apply a lambda function to each number ineven_numbersand squared them all, but this looks a bit long, right?

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

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

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

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

07:27What’s happening here is that thefilter() function filters out the odd numbersofnumbers and keeps the even numbers10,6, and50.

07:37Thenmap() is applying the lambda function on every number in the iteratorthat’s made by filter, so it takes10, squares it, takes6,squares it, and then takes50 and squares it.

07:50That’s why you got100,36, and2500.

07:55Congrats, you just learned how to combinemap() andfilter()!

Become a Member to join the conversation.

Course Contents

Overview
53%

[8]ページ先頭

©2009-2026 Movatter.jp