
Modern Python Cookbook
By :

Choosing a subset of relevant rows can be termed filtering a collection of data. We can view a filter as rejecting bad rows or including the desirable rows. There are several ways to apply a filtering function to a collection of data items.
In the Using stacked generator expressions recipe, we wrote the skip_header_date()
generator function to exclude some rows from a set of data. The skip_header_date()
function combined two elements: a rule to pass or reject items and a source of data. This generator function had a general pattern that looks like this:
def data_filter_iter(source: Iterable[T]) -> Iterator[T]:
for item in source:
if item should be passed:
yield item
The data_filter_iter()
function's type hints emphasize that it consumes items from an iterable source collection. Because a generator function is a kind of iterator, it will produce items of the same type for another consumer...