Iterators: next item please

range already feels lazy. iter/next is the protocol. A 50,000-row log should not all sit in RAM because the kiosk wanted twenty lines.

Weather station

Filter a table of readings.

You will be able to

  • Treat ‘next item please’ as a handshake, not a warehouse of rows
  • Walk a prefix of a long gate log without copying the rest
  • Know iter/next exists on a laptop once a for-loop is not enough

The kiosk asked for twenty lines, not fifty thousand

Gate 2’s overnight DHT log is huge. The technician on the kiosk wanted the first twenty heat lines. Loading every row into RAM is how a cheap Pi starts swapping. range already feels like the right policy: it does not build a giant tray of integers. Full Python names the handshake iter and next — ‘give me one more’. A for-loop already does that for you.

An iterable is the source (the list, the file, the range). An iterator is the finger that walks it. Two independent walks each start at the front. You practise a prefix here with a counter and a flag. On a laptop, for line in open(path): already streams. Do not call readlines() unless you truly need random access to every row.

Stop asking when the job is done

Lazy means compute when asked. It is right for logs, sensor streams and paginated weather JSON. It is wrong when accounts will sort the whole UPI table anyway — then load, then sort. Measure the job: prefix print versus full report.

This sandbox cannot run next(it). The flag-and-counter loop is the same decision: visit, maybe print, stop requesting once the prefix is full.

Words that matter

Iterable
The source you can walk — a list, a file, a range.
Iterator
The finger that produces the next value when asked.
Prefix
The first N items — often all a kiosk should load.

Common mistakes

Avoid: Reading an entire gate log into a list to print the first twenty lines.

Do this: Walk until N, then stop asking.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — a counter already feels lazy

range(4) is four turns. You never held four million turns.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — stop after a prefix

The source has more DHT rows. The kiosk only prints two.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Example program — First heat alert only

Once you have one alert, the rest of the file can wait.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Print two IDs

ids = ["A", "B", "C", "D"]. Print only A and B, then print Stopped.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. iter/next is the protocol for…
2. range(n) is a good mental model for…

Progress is stored in a browser cookie on this device.