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

Unlock This Lesson

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

Unlock This Lesson

Extract Even Numbers

00:00 Extract Even Numbers. Now that you understand how filter() works, let’s work on a new example. In this example, your goal is to extract even numbers from a list. First, let’s see how you can do this without filter(). You have a list of numbers here, [1, 3, 10, 45, 6, 50]. The extract_even() function takes a list of numbers as input and creates an empty list named even_numbers.

00:29 It then loops through each number in the input list and checks whether it’s even or not. To do this, you’re checking if each number’s remainder is 0 or not if you divide it by 2, and you’re using the % (modulo) symbol here, which is a percent symbol, that calculates the remainder of a division.

00:47 For example, 4 % 2 equals 0 since the remainder of four divided by two is zero. If the number is even—or, in other words, its % 2 is equal to 0—the number is added to the even_numbers list.

01:05 Finally, the function returns the even_numbers list containing only the even numbers from the input list. If you put in numbers in the extract_even() function, you get 10, 6, and 50 as your result. So what happened? Well, odd numbers 1, 3 and 45 got filtered out since their filtering condition evaluated as False. They’re not divisible by 2. For example, 3 % 2 is 1, not 0. Yet even numbers 10, 6, and 50 did not get filtered out since their % 2 is 0, making their filtering condition evaluate as True. Now, let’s do the same example but use filter() instead of for loops and if statements.

01:55 Your goal is to extract the even numbers and filter out the odd ones. Your filtering condition is if a number is divisible by 2, you need to return True and otherwise False.

02:10 Let’s start by creating the input list, just like before. You have a list of numbers here, [1, 3, 10, 45, 6, 50].

02:22 Next, let’s create a function named extract_even. It should take in a number and check whether it’s even or not. If it’s even, it should return True. If not, False.

02:33 Just a reminder that this function is also named as predicate or Boolean. So let’s do that. def is_even(number) as an input.

02:46 return Here you’re checking whether number is divisible by 2 or not, so let’s use % again. return number % 2 == 0.

03:01 For example, if you put in 3 here, 3 % 2 is 1 since three divided by two has a remainder of one. So 3 % 2 is not equal to 0, and is_even() returns False.

03:17 Now, let’s use filter() to apply is_even to every number in the numbers list. Also, just a reminder that filter() returns an iterator, so let’s call list() on its result in order to see the results on the console.

03:31 list(filter()). is even as filter’s function argument and numbers as its iterable argument. Now here you expect to see the even numbers, 10, 6, and 50.

03:46 Let’s see if it worked, and it did work. Okay, let’s see what happened here. The call to filter() applied is_even to every number in numbers and filtered out the odd numbers 0, 3, and 45. As a result, you get a list of even numbers, 10, 6, and 50.

04:06 As you can see, this code is way shorter and more efficient than its equivalent for loop.

Become a Member to join the conversation.