Menu

Python for Loops: Iterate Lists, Strings, Ranges, and Dicts

How Python's for loop works — iterating over lists, strings, ranges, dictionaries, and anything else that's iterable, plus break, continue, and else.

Python's for Loop Is "for each"

If you've used a C-style language, Python's for loop will look different. There's no for (i = 0; i < n; i++) counter pattern. Python's for is strictly a for each loop: you give it a collection and it hands you items one at a time.

main.py
Output
Click Run to see the output here.

That prints three lines. Python reads the list, pulls out each element in turn, and binds it to color while the body runs. When there's nothing left, the loop ends.

Notice the shape — for <name> in <iterable>: followed by an indented body. Indentation, colon, body; same structure as if.

What Counts as Iterable

Practically anything that holds a sequence of values. Lists, tuples, strings, ranges, dicts, sets, files, and most custom objects you'll build.

main.py
Output
Click Run to see the output here.

The uniformity is the nice part — one syntax, many data shapes.

Counting With range()

When you do want a counter — "do this ten times," or "loop through indices 0 to 9" — use range():

main.py
Output
Click Run to see the output here.

That prints 0, 1, 2, 3, 4. range(stop) counts from 0 up to but not including stop. range(start, stop) lets you set a starting value, and range(start, stop, step) lets you control the step size:

main.py
Output
Click Run to see the output here.

There's a dedicated page on range coming up because it's worth understanding a little more deeply. For now, range(n) gives you n iterations numbered 0 to n-1.

Python enumerate: Looping With an Index

A common need: "I want the index and the value together." The beginner instinct is to use range(len(...)), but Python has a nicer tool:

main.py
Output
Click Run to see the output here.

enumerate() yields (index, value) pairs. The double variable after for unpacks the pair into two names — index and color. Much cleaner than:

main.py
Output
Click Run to see the output here.

If you want to start counting at 1 instead of 0, pass start=1:

main.py
Output
Click Run to see the output here.

Python zip: Iterating Over Two Sequences Together

When you have two lists of equal length and want to pair them up:

main.py
Output
Click Run to see the output here.

zip stops at the shortest sequence. If you want it to keep going and fill with None, use itertools.zip_longest.

Iterating a Dict

Dictionaries give you choices about what to loop over:

main.py
Output
Click Run to see the output here.

Most of the time you want .items() — you get the key and the value together in one readable line.

break and continue

Sometimes you want to bail out early or skip over an iteration.

break exits the loop immediately:

main.py
Output
Click Run to see the output here.

Once a number above 6 is found, the loop stops. Remaining items are ignored.

continue skips the rest of the current iteration and moves to the next:

main.py
Output
Click Run to see the output here.

Both are fine to use, though continue can make loops harder to follow if you have more than one. Often a simple if/else reads better.

The else Clause on a Loop

Python has a feature most languages don't: an else clause on loops. It runs when the loop completes without hitting a break.

main.py
Output
Click Run to see the output here.

Useful, but initially confusing. Don't feel obligated to use it — a boolean flag outside the loop is often clearer for new readers.

Mutating a List While You Iterate

One trap worth flagging: don't add to or remove from a list while you're looping over it. The iteration gets confused about which indices are still valid, and you either miss items or trip an error. The safe pattern is to build a new list:

main.py
Output
Click Run to see the output here.

Or, once you're comfortable, a list comprehension does the same thing in one line — we'll cover that in the Collections chapter.

A Small End-to-End Example

Tie a few of these together — read a list, filter, enumerate, print:

main.py
Output
Click Run to see the output here.

Next: while Loops

for is the right choice when you know what you're iterating over. while is the right choice when you're looping until some condition changes — that's coming up next.

Frequently Asked Questions

How does a for loop work in Python?

for item in sequence: runs the indented body once for each element in the sequence, with item taking on each value in turn. Sequences include lists, tuples, strings, dicts, ranges, and any object that defines an iterator.

What's the difference between break and continue?

break exits the loop immediately, skipping any remaining iterations. continue skips the rest of the current iteration and moves on to the next one. break is for "I'm done"; continue is for "skip this one."

How do I loop with an index in Python?

Wrap the sequence in enumerate(...) to get (index, value) pairs: for i, item in enumerate(items):. Much cleaner than maintaining your own counter with i += 1 at the bottom of the loop.

Learn to code with Coddy

GET STARTED