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 With a Generator

00:00 In this lesson, you’ll start by revisiting generators, and then you’ll dive into the concept of replacing the filter() function with the power of generator expressions.

00:10 A generator is a special type of iterator in Python that generates values on the fly rather than storing them in memory like a list or tuple.

00:21 When Python programmers talk about generators, they either mean generator functions or generator expressions. In this lesson, you’ll work with generator expressions, but I’ll add you a resource that covers generator functions at the end of this course if you want to learn more about them. Okay, now let’s go ahead and talk about generator expressions.

00:45 You can see an example right here. It’s a generator expression that squares all the numbers from one to four, not including five. So (number ** 2 for number in range(1, 5)), not including 5.

01:02 Generator expressions are created using a syntax similar to list comprehensions, but with parentheses instead of square brackets. And they use lazy evaluation, meaning they generate values on the fly as they’re needed.

01:17 What we really mean here is that the calculations are not done immediately, but they’re done one at a time and only after they’re requested. Let’s figure all this out with an example.

01:33 You have the same exact generator expression here: (number ** 2 for number in range(1, 5)), not including 5. And this generator expression is stored in squares. Okay, so this generator expression is not immediately executed or evaluated.

01:52 It represents a blueprint for generating squared numbers when you need them. Now, how are you supposed to request values? You can request them with a for loop or the next() function.

02:07 Let’s try a for loop here. for each square in squares: print(square).

02:19 The results are given to you one at a time. You get 1, 4, 9, and 16, which are 1, 2, 3, and 4 squared. And as you can see, this is not a list or anything.

02:32 These are just values that have been requested one by one by the print() function in your for loop. Now you understand what lazy evaluation is and how generator expressions work.

02:48 Perfect. Now that your memory is fresh and you remember how generators work, let’s replace the filter() function with a generator expression.

02:57 Extract even numbers using a generator. You’ve already done this multiple times, and you’re familiar how to do this with filter(). Your goal is to replace filter() with a generator expression to filter out the odd numbers and keep the even ones.

03:15 You’re using the same list of numbers from the previous times you extracted even values, numbers = [1, 3, 10, 45, 6, 50]. Your filtering condition is the same, so you can use the same predicate function, is_even(). def is_even(), numberas an input. return number % 2 == 0.

03:38 So is_even() will return True when the remainder of a number is 0 when you divide it by 2 and False otherwise.

03:46 Let’s use a generator expression to extract the even numbers and store the generator object in even_numbers. even_numbers =. All you’re doing is storing the numbers, so store (number).

04:02 Now it’s time for the for loop—for each number in numbersbut only if the filtering condition, a.k.a. is_even(), is True for that number, so is_even(), number as an input.

04:18 Okay, now let’s check even_numbers. As you can see, it’s a generator object. Let’s view the result by calling the list() function on even_numbers.

04:30 The results is a list of numbers: [10, 6, 50], which are the even numbers of the numbers list. You could also use a for loop on even_numbers.

04:42 It doesn’t really make a difference. At the end, you get the same results, 10, 6, and 50. You just successfully replaced the filter() function with a generator expression and extracted the even numbers of a list.

Become a Member to join the conversation.