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
whileloop. - Once for each item in a collection →
for item in items:. You almost never needrange(len(items)). - You need the position too, or two lists side by side →
enumerate(items)orzip(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
itertoolsbefore 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.
Section
for loops and range
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
Section
Sorting and iteration tools
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
Section
Iterables and iterators
What a for loop actually calls, and why an iterator runs out.
Section
Generators and yield
A function that hands back one value at a time and pauses in between.
Section
Generator pipelines
Chain small generators so a big file flows through one line at a time.
Section
itertools and functools
Ready-made loops for chaining, batching, grouping and accumulating.
No Python loop at all
Section
Vectorization and broadcasting
One array expression replaces the loop and runs many times faster.