Complete worked example
python
from itertools import islice
def titles(rows):
for row in rows:
yield row["title"]
def take_titles(rows, count):
stream = titles(rows)
prefix = list(islice(stream, count))
return {"prefix": prefix, "rest": list(stream)}- Reasoning
- The same iterator carries its position from the prefix into the remainder.