1 00:00:00,940 --> 00:00:02,210 - [Instructor] In this video, we'd like you 2 00:00:02,210 --> 00:00:05,940 to experiment with the capabilities we just demonstrated 3 00:00:05,940 --> 00:00:09,740 with the filter and map functions in particular. 4 00:00:09,740 --> 00:00:12,700 So, for the purpose of the first exercise here, 5 00:00:12,700 --> 00:00:14,970 we'd like you to create a list called numbers 6 00:00:14,970 --> 00:00:17,890 that contains the values from one through 15, 7 00:00:17,890 --> 00:00:19,910 and then we've got three tasks for you 8 00:00:19,910 --> 00:00:23,460 to perform using that numbers list. 9 00:00:23,460 --> 00:00:25,700 In the first one, we'd like you to use a filter 10 00:00:25,700 --> 00:00:29,370 with a lambda to select only the even elements 11 00:00:29,370 --> 00:00:33,340 of numbers and to create a new list containing that result. 12 00:00:33,340 --> 00:00:36,320 In the second one, we'd like you to use the map function 13 00:00:36,320 --> 00:00:39,190 with the lambda to square all the values 14 00:00:39,190 --> 00:00:40,550 in numbers' elements. 15 00:00:40,550 --> 00:00:44,810 And then in the third, we'd like you to use filtering 16 00:00:44,810 --> 00:00:48,140 for numbers to get just the even elements, 17 00:00:48,140 --> 00:00:52,560 then map those even numbers to their squares. 18 00:00:52,560 --> 00:00:54,860 Now, we have a second exercise as well 19 00:00:54,860 --> 00:00:56,550 that we'd like you to try. 20 00:00:56,550 --> 00:01:00,140 We want you to map a list of Fahrenheit temperatures 21 00:01:00,140 --> 00:01:04,500 to a list a list of tuples, not just individual items, 22 00:01:04,500 --> 00:01:08,550 but tuple objects, that contain both those Fahrenheit values 23 00:01:08,550 --> 00:01:11,930 and their corresponding Celsius equivalents. 24 00:01:11,930 --> 00:01:16,070 So this is your equation for calculating Celsius 25 00:01:16,070 --> 00:01:17,710 from Fahrenheit temperatures. 26 00:01:17,710 --> 00:01:20,150 So we would like you to create a lambda 27 00:01:20,150 --> 00:01:23,370 that does that, and use it with mapping 28 00:01:23,370 --> 00:01:28,030 to create a new list containing those tuples described here. 29 00:01:28,030 --> 00:01:31,650 So go ahead and pause the video, and then come back 30 00:01:31,650 --> 00:01:32,993 to see the answers. 31 00:01:39,270 --> 00:01:42,140 Okay, let's go back to this first exercise. 32 00:01:42,140 --> 00:01:43,920 As you can see, we've already left 33 00:01:43,920 --> 00:01:47,480 for you the statements here for creating a list 34 00:01:47,480 --> 00:01:49,630 of values from one through 15, 35 00:01:49,630 --> 00:01:52,640 and evaluating that to see what it contains, 36 00:01:52,640 --> 00:01:54,320 just to make sure it worked correctly. 37 00:01:54,320 --> 00:01:56,900 So there's our numbers from one through 15. 38 00:01:56,900 --> 00:01:59,040 And again, the first thing we wanna do here 39 00:01:59,040 --> 00:02:02,160 is filter out only the even elements. 40 00:02:02,160 --> 00:02:03,680 So we wanna create a new list 41 00:02:03,680 --> 00:02:06,230 that contains just those even elements. 42 00:02:06,230 --> 00:02:07,530 And again, we could do this 43 00:02:07,530 --> 00:02:09,960 with list comprehensions, but here, we wanted you 44 00:02:09,960 --> 00:02:13,360 to specifically do it using the filter function. 45 00:02:13,360 --> 00:02:18,230 So if the value of a given element is divisible by two, 46 00:02:18,230 --> 00:02:21,440 the result will be zero, so we've created a lambda 47 00:02:21,440 --> 00:02:22,770 for that purpose. 48 00:02:22,770 --> 00:02:24,470 We're going to apply that lambda 49 00:02:24,470 --> 00:02:27,950 to each individual element in the numbers sequence, 50 00:02:27,950 --> 00:02:31,930 and the filter function will then give us back an Iterable 51 00:02:31,930 --> 00:02:35,720 of just the even numbers, which we will then pass to list, 52 00:02:35,720 --> 00:02:38,040 and it will iterate through the results, 53 00:02:38,040 --> 00:02:40,890 placing each item into the resulting list. 54 00:02:40,890 --> 00:02:44,130 So there is our set, or our list, rather, 55 00:02:44,130 --> 00:02:45,900 of the even-numbered values 56 00:02:45,900 --> 00:02:49,140 from the original sequence called numbers. 57 00:02:49,140 --> 00:02:51,890 Next, we wanted to do a mapping operation 58 00:02:51,890 --> 00:02:53,950 that would square each of those values. 59 00:02:53,950 --> 00:02:57,400 So, same basic concept here, but now we're calling map 60 00:02:57,400 --> 00:03:00,150 with a lambda, that instead of returning true or false, 61 00:03:00,150 --> 00:03:04,010 returns a new value for each individual item passed to it. 62 00:03:04,010 --> 00:03:07,560 So for whatever x is, we would like to square x, 63 00:03:07,560 --> 00:03:10,020 and we will apply that to each element 64 00:03:10,020 --> 00:03:11,760 of the numbers sequence. 65 00:03:11,760 --> 00:03:15,660 And the list function will then create the resulting list, 66 00:03:15,660 --> 00:03:17,810 containing all those values. 67 00:03:17,810 --> 00:03:19,540 Now, for the last operation, we wanted 68 00:03:19,540 --> 00:03:22,460 to combine those items so that we filter 69 00:03:22,460 --> 00:03:26,610 to get just the even numbers, then map those elements. 70 00:03:26,610 --> 00:03:30,220 So here, we have a list call that's going 71 00:03:30,220 --> 00:03:33,970 to receive the result of a mapping operation. 72 00:03:33,970 --> 00:03:37,870 The mapping operation is going to apply the map 73 00:03:37,870 --> 00:03:40,030 to the results of a filter operation. 74 00:03:40,030 --> 00:03:41,800 So let's start with that. 75 00:03:41,800 --> 00:03:45,080 We're going to filter to get just the even elements 76 00:03:45,080 --> 00:03:46,690 from numbers. 77 00:03:46,690 --> 00:03:49,890 Those elements will then be mapped to the squares 78 00:03:49,890 --> 00:03:53,330 of those elements, and those elements will then be used 79 00:03:53,330 --> 00:03:54,440 to create the list. 80 00:03:54,440 --> 00:03:58,760 So you can see just the squares of the even numbers 81 00:03:58,760 --> 00:04:01,330 that we had produced up above. 82 00:04:01,330 --> 00:04:04,070 Now separately, as we discussed previously, 83 00:04:04,070 --> 00:04:07,830 mapping does not have to create objects of the same type. 84 00:04:07,830 --> 00:04:10,570 In this case, we're going to take numeric values 85 00:04:10,570 --> 00:04:13,510 and turn them into tuples containing pairs 86 00:04:13,510 --> 00:04:14,540 of numeric values. 87 00:04:14,540 --> 00:04:17,760 So we're going to go from one numeric value to a tuple. 88 00:04:17,760 --> 00:04:19,330 That's a change in type. 89 00:04:19,330 --> 00:04:22,970 And then within the tuple, we will have numeric values. 90 00:04:22,970 --> 00:04:25,780 So first, we're going to create this Fahrenheit array, 91 00:04:25,780 --> 00:04:28,500 just so we have the values to process. 92 00:04:28,500 --> 00:04:31,680 And this is the expression that we're going to use, 93 00:04:31,680 --> 00:04:35,070 or the call that we're going to use, to produce the results. 94 00:04:35,070 --> 00:04:38,890 So starting with the map operation here, 95 00:04:38,890 --> 00:04:42,390 what we're saying is that for the elements of Fahrenheit, 96 00:04:42,390 --> 00:04:44,990 we would like to apply this lambda expression, 97 00:04:44,990 --> 00:04:47,740 which is going to take each Fahrenheit temperature 98 00:04:47,740 --> 00:04:49,500 and then produce a tuple. 99 00:04:49,500 --> 00:04:52,220 Notice the parentheses wrapped around this here. 100 00:04:52,220 --> 00:04:55,690 Going to produce a tuple containing the value of x, 101 00:04:55,690 --> 00:04:57,690 which is the Fahrenheit temperature, 102 00:04:57,690 --> 00:05:01,070 separated from the next item, 103 00:05:01,070 --> 00:05:04,910 which will be the corresponding Celsius value produced 104 00:05:04,910 --> 00:05:07,190 with the calculation you see up above here. 105 00:05:07,190 --> 00:05:10,700 So Fahrenheit minus 32 times 5/9ths 106 00:05:10,700 --> 00:05:13,560 will give us the corresponding Celsius temperature. 107 00:05:13,560 --> 00:05:16,460 And then the list function will take those mapped values 108 00:05:16,460 --> 00:05:19,490 and create a list of tuples for us. 109 00:05:19,490 --> 00:05:22,910 So we see here, 41 maps over to five, 110 00:05:22,910 --> 00:05:27,150 32 maps to zero in Celsius, and 212 maps 111 00:05:27,150 --> 00:05:30,183 to 100 in Celsius degrees.