Python generators
How yield turns a function into a lazy sequence: generator functions and expressions, how they save memory on large data, iterators and next(), pipelines of generators, infinite sequences, yield from, and the one-pass catch.
CodexBash, , 4 min read
A list holds every value in memory at once. A generator produces values one at a time, only when asked. That lets a few lines of Python process a file larger than your RAM, stream results as they are found, or describe an infinite sequence. The keyword that makes it happen is yield, and once you understand it, a lot of Python, from range to open to zip, makes more sense.
Sign in to read the rest
The whole 4 minute article is free with an account, along with every problem, lesson and tutorial. No card.