Need Assistance?

support@tutorialshub.in

Python Standard Library

collections module

ADVERTISEMENT
Python Free Lesson
5 min read
ADVERTISEMENT

Iteration as a protocol

An iterable can provide an iterator, and an iterator produces values one at a time. A for loop hides this protocol, but understanding it explains why lists, files and generators can all be iterated.

Generators save memory

A generator function uses yield to pause and resume execution. Instead of constructing the complete result, it produces one value when the consumer requests it.

Build processing pipelines

Generators are useful for large files, database streams and transformations where data can be processed incrementally. Remember that a generator is normally consumed once.

Practice: create a generator that reads a large log file and yields only lines containing ERROR.

ADVERTISEMENT