15
Lesson 20 of 44

Iterators and generators

Discover how the for loop works internally and how to produce values one at a time, without storing them all in memory. In this lesson you will learn what iterables and iterators are, how to write generator functions with yield and yield from, how to work with infinite generators and itertools.islice, how to chain generators and how to create your own iterator classes with __iter__ and __next__.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Iterables and iterators

You have used for hundreds of times, but what really happens when you write for x in items ? Understanding it is the basis of…

Theory
Read more

Generator functions with yield

Writing an iterator by hand is laborious. Python offers a shortcut: generator functions . A function that uses yield instead of…

Theory
Read more

Infinite generators and generator expressions

Since a generator calculates values on demand, it can be infinite : it never runs out of values and never fills the memory…

Theory
Read more

Generator pipelines

One of the best ideas with generators is chaining them: the output of one is the input of the next, like a pipeline. Each value…

Theory
Read more

Iterator classes: __iter__ and __next__

Generators solve almost every case. But sometimes you want your own objects to be iterable with for . For that you have to…

Theory
Read more

Practice exercises 10

Looping with iter and next

Create a Python program that goes through a list by hand , the way the for loop does internally.

Exercise Easy
Solve it

Countdown with a generator

Create a Python program with your first generator function , which uses yield instead of return .

Exercise Easy
Solve it

Fibonacci with a generator

Create a Python program with a generator that produces the first terms of the Fibonacci sequence.

Exercise Easy
Solve it

Lines without comments

Create a Python program with a generator that filters the lines of a text, processing them one at a time.

Exercise Medium
Solve it

Flatten with yield from

Create a Python program with a generator that joins several lists into a single sequence using yield from .

Exercise Medium
Solve it

Infinite primes

Create a Python program with an infinite generator of prime numbers and take only the first ones with itertools.islice .

Exercise Medium
Solve it

Collatz sequence

Create a Python program with a generator that produces the Collatz sequence, a generator whose length is not known beforehand.

Exercise Medium
Solve it

First element that matches

Create a Python program that finds the first element that meets a condition with a generator expression and next() , without…

Exercise Easy
Solve it

Custom range

Create a Python program with an iterator class : an object that can be looped over with for because it implements __iter__ and…

Exercise Medium
Solve it

Generator pipeline

Create a Python program that chains several generators, so that each value goes through all the stages before moving on to the…

Exercise Medium
Solve it