10 Generators and Comprehensions
In the previous chapter, we escaped all the headaches of traditional index-based loops. However, we haven’t yet completely escaped the nested loop.
The solution is to employ generator expressions, which allow you to rewrite the entire logic of a loop in a single statement. You can even create lists in this manner, with the much-loved list comprehensions. Before I get there, I’ll introduce generators, which provide a more compact alternative to custom iterable classes in many situations. You’ll also encounter the oft-overshadowed cousin of the generator, the humble simple coroutine, which provides an iterative ...
Get Dead Simple Python 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.