6/28
Part 2 of 6 · Filling a rowLearn and do

Fill a slot with the row's best title that isn't taken

Now the fix. A row fills its slots one at a time, left to right, and each slot takes the best title the row still has that isn't on the screen. "Best" means first in the row, so the slot takes the first free title in the row's order.

next with a default says it in one line:

row, taken = ["B", "A", "D", "E"], {"A", "B"}
print(next((title for title in row if title not in taken), None))
print(next((title for title in row if title not in {"A", "B", "D", "E"}), None))
D
None

The generator walks the row and yields only the free titles. next takes the first one. The None matters: without a default, a row with nothing free raises StopIteration, and that error surfaces far from the cause.

Predict

Row 1 of PAGE is B, A, D, E, and the screen holds A and B. Which title does its slot 0 get?

Pick an answer above to see the rest