Skip to main content
Python & FastAPI Interviews Theory P0 Medium

Iterators, generators, yield and memory efficiency

Explain the iterator protocol, lazy evaluation with yield, and when generators beat lists.

Official Reference

Model Answer & Explanation

An iterable implements __iter__ returning an iterator; an iterator implements __next__ and raises StopIteration when exhausted. Generator functions compile into state machines that pause at yield, holding only the current item in memory instead of materializing an entire list—ideal for streaming files, DB cursors, and LLM SSE chunks.

Follow-up Interview Question · What happens if you iterate over the same generator object twice?

Module Context & References

Python & FastAPI · Popular interview theory The 20% of Python & FastAPI concepts asked in 80% of backend interviews
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.