9.2 Applying transformations to a collection
We often define generator functions with the intention apply the function to a collection of data items. There are a number of ways that generators can be used with collections.
In the Writing generator functions with the yield statement recipe in this chapter, we created a generator function to transform data from a string into a more complex object.
Generator functions have a common structure, and generally look like this:
def new_item_iter(source: Iterable[X]) -> Iterator[Y]:
for item in source:
new_item: Y = some_transformation(item)
yield new_item
The yield statement means the results will be generated iteratively. The function’s type hints emphasize that it consumes items from the source collection. This template for writing a generator function...