Generators and Yield

Updated May 4, 2026

Short answer

Memory-efficient iterators.

Deep explanation

Functions that use 'yield' to return values one by one, maintaining state between calls instead of storing a full list in RAM.

Real-world example

Reading a 10GB log file line-by-line without crashing the server.

Common mistakes

  • Trying to access generator elements by index like a list.

Follow-up questions

  • What is next()?

More Python interview questions

View all →