You’ll be able to
- Understand the difference between iterables and iterators
- Build custom iterators with
__iter__and__next__ - Use
iter()andnext()explicitly for finer control - Chain iterators with
itertoolsfunctions
Loading lesson…
Why this matters
itertools, pandas' .iterrows(), and every streaming parser (csv.reader, json.JSONDecoder.raw_decode) rely on the iterator protocol. Staff engineers prefer generators over materialized lists for log processing and ETL pipelines because they compose lazily and hold O(1) memory over billion-row streams.
Common pitfalls
- Iterating the same iterator twice — it's exhausted after one pass; wrap in
list()or useitertools.tee. - Forgetting to
raise StopIterationin a manual__next__— infinite loops and hungforblocks follow. - Using
.iterrows()on large DataFrames — 100× slower than vectorized ops; reach for.itertuples()or vectorization.