Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Every way to loop over data

while, for, enumerate and zip, a comprehension, a generator, itertools, or no loop at all with NumPy. What you want out of the loop decides which.

Ask what the loop is for, then pick the plainest tool that does it.

  • You don't know in advance how many times (keep asking until the input is valid, keep going until a value settles) → a while loop.
  • Once for each item in a collection → for item in items:. You almost never need range(len(items)).
  • You need the position too, or two lists side by side → enumerate(items) or zip(a, b).
  • You're building a new list, set or dict from an old one → a comprehension: [f(x) for x in xs if test(x)].
  • The data is huge, endless, or arrives a piece at a time → a generator, so only one item is in memory at once.
  • The loop is a well-known pattern (chunk, pair up, group, chain, running total) → look in itertools before writing it yourself.
  • It's arithmetic over many numbers → a NumPy array operation, which runs the loop in compiled code instead of in Python.

Start at the top. Move down only when the plainer form gets awkward.

The two loop statements

Section

while loops

Repeat while a condition holds; the body must change something the condition reads.

One pass per item; range counts when you really do need numbers.

Section

Loop patterns

Count, total, find, filter and best-so-far, the shapes most loops take.

Loops that build something

enumerate, zip, sorted, reversed, any and all instead of index bookkeeping.

Section

Comprehensions

A new list, set or dict in one expression, with an optional filter.

Lazy loops

What a for loop actually calls, and why an iterator runs out.

A function that hands back one value at a time and pauses in between.

Chain small generators so a big file flows through one line at a time.

Ready-made loops for chaining, batching, grouping and accumulating.

No Python loop at all

One array expression replaces the loop and runs many times faster.