for Loops

Repeat once for each item

Beginner 10 min

In this lesson

A for loop walks through a collection — a list, a string, a range of numbers — and runs your code once for each item. It’s the workhorse of Python, and you’ll use it constantly.

Explain it like I’m 5

‘For each thing in this pile, do something with it.’ Python hands you the items one at a time so you can deal with each one.

Looping over a collection

The pattern is for item in collection:. On each pass, the loop variable (here item) is automatically set to the next element. You can loop over lists, strings (character by character), dictionaries, and more — anything Python calls iterable.

Example
fruits = ["apple", "pear", "kiwi"]

for fruit in fruits:
    print("I like", fruit)
Output
I like apple
I like pear
I like kiwi
Once per item, in order.

Counting with range()

When you want to repeat a fixed number of times, use range(). range(5) produces 0, 1, 2, 3, 4 (it starts at 0 and stops before the end). range(1, 6) gives 1–5, and range(0, 10, 2) counts by twos. range is the idiomatic way to say ‘do this N times’.

Example
total = 0
for n in range(1, 6):
    total = total + n
print("Sum 1..5 is", total)
Output
Sum 1..5 is 15
Using range() to add up the numbers 1 through 5.

Write a loop that prints the 3-times table from 3×1 to 3×10. Run it and check the last line is 3 x 10 = 30.

for n in range(1, 11):
    print("3 x", n, "=", 3 * n)

Knowing the position with enumerate()

Sometimes you need both the item and its index. Rather than managing a counter by hand, wrap the collection in enumerate(): for index, item in enumerate(names): gives you both each pass. It’s cleaner and less error-prone than tracking the index yourself.

Example
fruits = ["apple", "pear", "kiwi"]

for index, fruit in enumerate(fruits):
    print(index, fruit)
Output
0 apple
1 pear
2 kiwi
enumerate() gives you the index and the item together.

Nested loops

A loop can contain another loop. For every pass of the outer loop, the inner loop runs completely. This is how you process grids, tables, or all pairs of things — but each level of nesting multiplies the work, so keep an eye on how many total iterations you’re creating.

Example
for row in range(1, 3):
    for col in range(1, 4):
        print(row, col)
Output
1 1
1 2
1 3
2 1
2 2
2 3
The inner loop runs fully for each pass of the outer loop.

Looping over two lists with zip(), and reversed()

Two built-ins make for loops more expressive. zip(a, b) walks two (or more) lists in parallel, handing you one item from each on every pass — perfect when related data lives in separate lists. reversed(seq) walks a sequence back to front.

(When your goal is to transform a list into a new one rather than just visit it, a list comprehension is often cleaner — that has its own lesson.)

Example
names = ["Ada", "Bo", "Cy"]
scores = [90, 75, 88]

for name, score in zip(names, scores):
    print(name, "scored", score)

for n in reversed([1, 2, 3]):
    print(n)
Output
Ada scored 90
Bo scored 75
Cy scored 88
3
2
1
zip pairs items up; reversed walks backwards.

Common mistake: Off-by-one errors with range()

Why it happens:

range excludes its stop value, so range(1, 5) gives 1–4, not 1–5. It’s easy to forget the end is not included.

How to fix it:

Remember: to include N, write range(start, N + 1). When in doubt, print the range as a list to see exactly what it produces.

Common mistake: Modifying a list while looping over it

Why it happens:

Adding or removing items from the same list you’re iterating confuses Python’s internal position tracker and skips or repeats items.

How to fix it:

Loop over a copy (for x in items[:]:) or build a new list instead of changing the one you’re iterating.

What numbers does range(2, 7) produce?

Why use enumerate() in a for loop?

Mini exercise (medium)

Given words = ["red", "green", "blue"], print each word with its position starting at 1, like 1: red.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

words = ["red", "green", "blue"]
lines = []
# TODO: build "1: red", "2: green", "3: blue" (number from 1)
for word in words:
    lines.append(word)
print(lines)

What to learn next

You practised for loops: iterating over lists and strings, counting with range(), using the loop variable, numbering items with enumerate(), and nesting loops. You used enumerate() to label a list of colours as “1: red”, “2: green”, “3: blue”.

A for loop repeats a known number of times; sometimes you need to repeat until something changes. while Loops covers exactly that, including how to avoid infinite loops and the common “keep asking until the answer is valid” pattern. When you’re ready, continue to the next lesson.