Iterators

Iterator example:

Generators

Example: creating a generator from an iterator using generator comprehension

Example: creating an infinite sequence

Example: generator expressions

(They are well suited in cases where memory is an issue, but then can be slower than list expressions.)

Example: reading a file line by line

Example: data processing pipeline

Example: implementing a for loop

Example: implementing range()

(From the Python Cookbook recipee 4.3)

Example: creating an arithmetic progression sequence

yield from

We can split it into two

yield from allows us to simplify the above.

Merging two sorted collections

Sources