1 00:00:00,000 --> 00:00:02,009 Python has this function called 'filter', 2 00:00:02,020 --> 00:00:06,000 and filtering is really just a way to apply a function and 3 00:00:06,000 --> 00:00:08,600 to remove data that you don't want, just like the name intends. 4 00:00:08,640 --> 00:00:12,090 So if you wanted every second number, every even number, 5 00:00:12,560 --> 00:00:14,630 just vowels, you could do that. 6 00:00:14,640 --> 00:00:19,550 And you could apply a filter against some sort of data type. 7 00:00:19,560 --> 00:00:23,630 So just like the 'map' function, it takes a function name ,and 8 00:00:23,640 --> 00:00:26,270 then some sort of iterable, something that we can loop over, 9 00:00:27,600 --> 00:00:29,600 and if you were to run something like this, you're going to 10 00:00:29,600 --> 00:00:32,900 see that there is some sort of memory point allocated to it. 11 00:00:32,939 --> 00:00:36,080 And so you can't actually store this in a variable because 12 00:00:36,080 --> 00:00:38,400 it's just going to be a piece of memory. 13 00:00:38,400 --> 00:00:41,300 So let's go ahead and create an example here where we just 14 00:00:41,300 --> 00:00:43,000 want all the even numbers. 15 00:00:43,000 --> 00:00:49,300 So 'def get_even_numbers()', and then it's going to take a 'num' 16 00:00:49,300 --> 00:00:51,200 [no audio] 17 00:00:51,200 --> 00:00:52,400 and this is not called 'get', 18 00:00:52,400 --> 00:00:54,900 let's call it 'filter_even_numbers', 19 00:00:54,900 --> 00:00:59,000 and we're going to give this a Docstring "Returns only even 20 00:00:59,000 --> 00:01:03,020 numbers". And what we can do here, is we can return whatever 21 00:01:03,020 --> 00:01:04,099 that 'num' is going to be, 22 00:01:04,900 --> 00:01:08,200 modulus, '% 2 = 0'. 23 00:01:09,900 --> 00:01:14,200 Now, the thing with the 'filter' function here, is if it returns 24 00:01:14,200 --> 00:01:17,800 True, then it's going to keep the data, whatever this value is 25 00:01:17,800 --> 00:01:21,300 going to be, but if it returns False, it's going to discard it. 26 00:01:21,320 --> 00:01:23,270 We'll see this in action in just a little bit. 27 00:01:23,280 --> 00:01:26,530 And with this modulus, what this is saying is, if there's 28 00:01:26,530 --> 00:01:30,100 a number, let's say 5 divided by 2, is there a remainder? 29 00:01:30,180 --> 00:01:33,210 And if there is no remainder, if that remainder is 0, 30 00:01:33,380 --> 00:01:34,790 then return True. 31 00:01:35,500 --> 00:01:38,500 And that's what this modulus, this '%' sign is, that's 32 00:01:38,500 --> 00:01:41,000 called modulus, and so that really just gets the remainder. 33 00:01:42,100 --> 00:01:43,300 So let's go ahead and run that. 34 00:01:44,020 --> 00:01:46,120 And let's create a list of numbers in here. 35 00:01:46,130 --> 00:01:53,900 So 'nums', we've got 1-2-3-4-5-6, all the way to whenever 36 00:01:53,900 --> 00:01:56,700 we want to stop, so 14, I guess. 37 00:01:56,700 --> 00:01:59,100 And now what we can do, is we can loop through this, 38 00:01:59,100 --> 00:02:02,800 so we can say 'for num in filter', 39 00:02:04,400 --> 00:02:08,300 it's going to take the name of the function, 'filter_even_numbers', 40 00:02:08,300 --> 00:02:11,500 and then the 'nums' itself. 41 00:02:11,560 --> 00:02:16,220 So the 'nums' is referring to the iterable, it's a list. 42 00:02:16,230 --> 00:02:17,180 So we can loop through it, 43 00:02:17,180 --> 00:02:18,200 it's called iterable. 44 00:02:18,200 --> 00:02:21,900 And 'filter_even_numbers' is referring to this function without 45 00:02:21,900 --> 00:02:25,700 the parentheses. It knows to run this function on its own internally 46 00:02:25,710 --> 00:02:27,410 so we don't need to execute it right now. 47 00:02:27,500 --> 00:02:29,720 And then we could simply print the number. 48 00:02:29,730 --> 00:02:32,690 Now, what this is going to do is, it's going to put number 49 00:02:32,690 --> 00:02:35,600 1 in here. Is 1 divided by 2, 50 00:02:35,600 --> 00:02:37,900 does that have any sort of remainder? 51 00:02:37,980 --> 00:02:39,300 What about 2? 52 00:02:39,300 --> 00:02:41,100 2 divided by 2, does that have a remainder? 53 00:02:41,100 --> 00:02:42,900 3 divide by 2, does that have a remainder? 54 00:02:42,900 --> 00:02:45,200 4 divided by 2, and it'll just do that over and over and over again. 55 00:02:45,220 --> 00:02:46,500 So let's go ahead, print this, 56 00:02:46,500 --> 00:02:48,600 and now we have just even numbers. 57 00:02:48,600 --> 00:02:51,500 2-4-6-8-10-12, and 14. 58 00:02:51,560 --> 00:02:54,260 Now, what happens if we just run this on its own? 59 00:02:54,260 --> 00:02:58,900 'filter(filter_even_numbers())', 60 00:02:58,900 --> 00:03:03,900 with 'nums' in here. Again, we get a memory location just like 61 00:03:03,900 --> 00:03:05,600 the 'map' function gave us. 62 00:03:05,640 --> 00:03:07,860 We can't even store this as a variable. 63 00:03:07,870 --> 00:03:13,340 So let's say 'evens =', again, it's just, it's saying that 64 00:03:13,350 --> 00:03:17,900 this is a 'filter', and is pointing to a piece of memory, 65 00:03:17,900 --> 00:03:19,580 and we can't do anything with that. 66 00:03:19,680 --> 00:03:24,070 But just like with 'map', what we can do, is cast this to a 67 00:03:24,070 --> 00:03:27,190 'list', and if we do that, there's some magic behind the scenes 68 00:03:27,200 --> 00:03:30,600 here. It will automatically give us a list of all the numbers. 69 00:03:32,200 --> 00:03:35,200 And so essentially, what we've done is we've created a function 70 00:03:35,280 --> 00:03:39,940 called 'filter_even_numbers', and we gave it something to loop 71 00:03:39,940 --> 00:03:42,900 through, and it looped through 1-2-3-4, all the way up to 72 00:03:42,900 --> 00:03:43,900 13 and 14, 73 00:03:43,900 --> 00:03:47,700 and it applied this logic to it every single time. 74 00:03:47,720 --> 00:03:52,220 Now, if this logic came back True, we had a number. 75 00:03:52,230 --> 00:03:54,890 If the logic came back False, it was stripped out. 76 00:03:55,080 --> 00:03:56,820 Now, where is this useful? 77 00:03:56,830 --> 00:04:00,540 Actually, even numbers or odd numbers it is quite useful. 78 00:04:00,550 --> 00:04:01,740 I've seen that quite a bit. 79 00:04:01,740 --> 00:04:06,100 You can also strip out characters that aren't a vowel or 80 00:04:06,100 --> 00:04:07,600 they are a consonant, 81 00:04:07,600 --> 00:04:10,300 or if you're dealing with a big website, you could even filter 82 00:04:10,310 --> 00:04:12,040 through users that have a certain permission. 83 00:04:12,040 --> 00:04:15,000 So you could get all these users, and then you can loop through each one, 84 00:04:15,050 --> 00:04:17,779 you can check to see if they're an admin, and just get a list 85 00:04:17,779 --> 00:04:20,300 of admin users, and just return User IDs, 86 00:04:20,300 --> 00:04:21,700 you could do something like that as well. 87 00:04:21,700 --> 00:04:25,000 So this gets to be very, very useful down the road. 88 00:04:25,040 --> 00:04:27,320 Let's go ahead and give 'filter' a shot, 89 00:04:27,320 --> 00:04:28,370 give 'map' a shot. 90 00:04:28,370 --> 00:04:29,533 [no audio]