Flattening data while mapping

In Chapter 4, Working with Collections, we looked at algorithms that flattened a nested tuple-of-tuples structure into a single iterable. Our goal at the time was simply to restructure some data, without doing any real processing. We can create hybrid solutions that combine a function with a flattening operation.

Let's assume that we have a block of text that we want to convert to a flat sequence of numbers. The text looks as follows:

>>> text= """\
...   2   3    5    7   11   13   17   19   23   29 
...  31  37   41   43   47   53   59   61   67   71 
...  73  79   83   89   97  101  103  107  109  113 
... 127 131  137  139  149  151  157  163  167  173 
... 179 181  191  193  197  199  211  223  227  229 
... """  

Each line is a block of 10 numbers. We need to unblock the rows ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.