In this lesson, you’ll learn about generators. Generators are a useful way to iterate through a sequence using constant memory. Here’s an example:
>>> g = (x for x in [1, 2, 3]) >>> g <generator object> >>> next(g) 1 >>> next(g) 2 >>> next(g) 3 >>> next(g) StopIteration
You can also define generators using a function:
>>> def f(): ... yield 1 ... yield 2 ... yield 3 >>> f() <generator object> >>> g = f() >>> next(g) 1 >>> next(g) 2 >>> next(g) 3 >>> next(g) StopIteration
If you want to learn more, check out What Are Python Generators?
00:00 Generators are a special type of iterator that you can use to iterate over a sequence of values. They’re special because they’re lazily evaluated—that means that you only evaluate values when you need them.
Let’s create a generator to iterate over the values
g = and then it’s like a list comprehension, except you use parentheses so that this is a generator expression.
(i for i in range(6))—because we want to include
range() is exclusive.
g is a generator.
To get the next value, you type
5. If you call
next() again, it will raise a
StopIteration because there are no more values to iterate through. What’s special about a generator is that once it’s done—as in, once we’ve exhausted all the values, you can’t get the values again—calling
next() doesn’t give you anything. It will always raise an error.
So to get the values again, you have to instantiate it again, and then call
next() again. Let’s compare this with a list. Let’s say we want to sum all the values between
1000. Using a list, you would do
sum(), list comprehension—
[i for i in range(1, 1001)].
This is because
sum(), under the hood, calls
__iter__() on our generator, which actually just returns the generator, but I’ll save it into a variable called
iterator. Then, it calls the
next() over and over on our generator.
g is only 128 bytes. So you can imagine, as our list grows really large—millions and millions of values—this is going to take up a lot more memory. Generators, on the other hand, will always be this constant number of bytes.
02:32 So in an interview, if you’re asked to loop through something that might contain lots and lots of values, you should use a generator. You’d also want to use a generator if you don’t need to evaluate all the values at once—you just need to evaluate them one at a time.
if you had, like, some
slow_method(), or something, that you call with
i on each time, this will evaluate the
slow_method() for all the values at once and then proceed with the code, while a generator, like this, would only evaluate the
slow_method() when you need to, like this.
f(), you would think according to most rules would go inside the function, but this actually returns a generator object. To go inside, you have to call
next() on that generator object, and you have to actually save this into a variable, and then call
f() like this is going to create a new generator each time. There are other examples of generator functions, and I will link a Real Python video on generators down below. In the next video, you’ll learn about dictionaries and
defaultdicts, which are a very useful data structure to store default values.
Become a Member to join the conversation.