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…
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__.
You have used for hundreds of times, but what really happens when you write for x in items ? Understanding it is the basis of…
Writing an iterator by hand is laborious. Python offers a shortcut: generator functions . A function that uses yield instead of…
Since a generator calculates values on demand, it can be infinite : it never runs out of values and never fills the memory…
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…
Generators solve almost every case. But sometimes you want your own objects to be iterable with for . For that you have to…
Create a Python program that goes through a list by hand , the way the for loop does internally.
Create a Python program with your first generator function , which uses yield instead of return .
Create a Python program with a generator that produces the first terms of the Fibonacci sequence.
Create a Python program with a generator that filters the lines of a text, processing them one at a time.
Create a Python program with a generator that joins several lists into a single sequence using yield from .
Create a Python program with an infinite generator of prime numbers and take only the first ones with itertools.islice .
Create a Python program with a generator that produces the Collatz sequence, a generator whose length is not known beforehand.
Create a Python program that finds the first element that meets a condition with a generator expression and next() , without…
Create a Python program with an iterator class : an object that can be looped over with for because it implements __iter__ and…
Create a Python program that chains several generators, so that each value goes through all the stages before moving on to the…