For-Loops

lecture
python
for-loops

Lecture 5A. See csse1001 for course logistics and python-for-loops for the full reference on for-loop syntax, ranges, and enumerate.

Outline

  • For-loops: definition and syntax
  • String iteration, character by character; nesting for-loops
  • The accumulator pattern
  • For-loops vs. while-loops — which can replace which
  • List looping, index by index; out-of-place vs. in-place
  • range (iterating through numbers) and enumerate (iterating with index)
  • Dictionary looping, key by key

Definitions

Loop — a control structure that repeats code that belongs to it.

For-loop — a control structure that, given a group, repeats code for every member of that group, in order.

for <name> in <iterator>:
    <code>

String iteration (character by character)

A for-loop iterates through a string’s characters, in order:

>>> for x in "abcd":
...     print(x)
a
b
c
d
>>> x
'd'

The looping name (x) retains its final value after the loop exits.

Nesting for-loops

>>> (digits, alphas) = ("012", "xy")
>>> for d in digits:
...     for a in alphas:
...         print(d + a)
0x
0y
1x
1y
2x
2y

Python disallows empty for-loop bodies — use pass (the empty instruction, which has no effect) as a placeholder:

>>> for d in digits:
...     for a in alphas:
...         pass
...     print(d + a)
0y
1y
2y

Again, the names used to iterate through an iterator retain their values after the for-loop exits — by the time the outer loop’s print runs, the inner loop has already finished, leaving a at its last value ('y') each time.

Pulling print out of both loops leaves it running exactly once, on whatever d and a were left holding:

>>> (digits, alphas) = ("012", "xy")
>>> for d in digits:
...     for a in alphas:
...         pass
... print(d + a)
2y

So the same two loops give six lines, three lines or one line depending only on where print is indented.

Figure 1: Where the print statement is indented decides how many times it runs. The tree is the order the two nested loops visit their six (d, a) combinations, left to right. The three variants (6 lines, then 3, then 1) are keyed by the box around each printed value — none, purple, blue — and by nothing else: the purple traversal-order label beneath the tree is an annotation, not a fourth entry in that key.

The accumulator pattern

An accumulator is a variable a loop uses to build up an aggregate value:

>>> acc = ""
>>> for x in "abcd":
...     acc = acc + x
...     print(acc)
a
ab
abc
abcd

Building it in reverse order just by swapping the order of concatenation:

>>> acc = ""
>>> for x in "abcd":
...     acc = x + acc
...     print(acc)
a
ba
cba
dcba

The loop visits 'a', 'b', 'c', 'd' in the same order both times — only the side the new character is joined on changes, and that alone reverses the result:

Figure 2: Why acc = acc + x builds abcd and acc = x + acc builds dcba. The top row, marked before the loop, is the empty accumulator each snippet sets up on its first line — not a pass, and never printed; each of the four rows below it is one pass, and the magenta cell is the character just added. Those four rows per column are exactly the printed output of the two loops above.

For-loops vs. while-loops

Can every for-loop be rewritten with only while-loops? Yes:

>>> for x in xs:
...     ...   # do something with x

is code-equivalent to:

>>> k = 0
>>> while k < len(xs):
...     ...   # do something with xs[k]
...     k += 1

Can every while-loop be rewritten with only for-loops? No — consider how many times you’d need to repeat asking a user for correct input (not known in advance). That is exactly the validation loop from 2025-08-18-while-loops: the trip count is not known until the user cooperates, so there is no group to iterate over.

Technical caveat: a for-loop can be tricked into looping forever in Python, by giving it an iterator that never exhausts. And if you dig deep enough, Python itself used a while-loop to build that iterator in the first place.

Exercise: removing characters

def remove(c: str, cs: str) -> str:
    """ Return the string with all instances of c removed from cs.
    Precondition: len(c) == 1
    >>> remove('b', "bluey")
    'luey'
    >>> remove('b', "Bluey")
    'Bluey'
    >>> remove('b', "chilli")
    'chilli'
    >>> remove('b', "")
    ''
    """

The shape is the accumulator pattern: accumulate into acc, skipping the character to be removed. The loop variable needs its own name (x), compared against the parameter c. Reusing c as the loop variable would shadow the parameter it is supposed to be compared against.

Fill in the blank to complete remove:

acc = ""
for x in cs:
    if x != c:
        acc += x
return acc

List looping (index by index)

A for-loop also iterates directly through a list’s elements. This example uses no index at all — indices arrive later, with range and enumerate.

>>> xs = ['a', 2, 'c', 4, 'e']
>>> for x in xs:
...     print(x)
a
2
c
4
e

Exercise: unique elements

def make_unique(xs: list[int]) -> list[int]:
    """ Returns the UNIQUE elements of xs.
    >>> make_unique([1, 2, 3, 4])
    [1, 2, 3, 4]
    >>> make_unique([1, 2, 2, 2, 3, 1, 4, -3])
    [1, 2, 3, 4, -3]
    """
acc = []
for x in xs:
    if x not in acc:
        acc.append(x)
return acc

This is an out-of-place computation — the original list xs is not modified. An in-place change would instead modify xs directly; those are covered later, in the object-oriented part of the course.

Ranges (iterating through numbers)

range builds an iterator of numbers, with the general form range([start], stop[, step]) (square brackets denote optional arguments) — similar to list slicing:

>>> range(10)
range(0, 10)
>>> type(range(10))
<class 'range'>
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2, 7))
[2, 3, 4, 5, 6]
>>> list(range(2, 7, 3))
[2, 5]
>>> list(range(2, 7, -1))
[]
>>> list(range(7, 2, -1))
[7, 6, 5, 4, 3]

The rule for reading the last four follows the slicing analogy: the walk starts at start, hops by step, and yields every value it lands on that is still short of stop in the direction it is travelling. So stop is never produced, and start is produced only when the walk produces anything at all: range(2, 7, -1) is empty because a negative step walks down from 2, away from 7, and 2 is already past 7 going that way, so not even start comes out. See python-for-loops for a diagram of those four.

range is commonly used to walk through a list by index:

>>> xs = [11, 22, 33, 44]
>>> for k in range(len(xs)):
...     ...   # code involving xs[k]

Enumerate (iterating with index)

enumerate simplifies the index-and-value pattern above. Roughly:

enumerate([x0, ..., xn]) == [(0, x0), ..., (n, xn)]

(this isn’t the full story — enumerate actually returns an iterable, not a list — but the pairs it produces work the same way):

>>> xs = [1, 2, 3, 4]
>>> for k, xk in enumerate(xs):
...     ...   # code involving k and xk == xs[k]

Each (k, xk) pair is a tuple, an immutable list (see python-lists):

>>> for k, x in enumerate(["Scientia", "ac", "Labore"]):
...     print(k, x)
0 Scientia
1 ac
2 Labore

(“Scientia ac Labore” is UQ’s motto — “by means of knowledge and hard work”.)

Dictionary looping (key by key)

A for-loop iterates through a dictionary’s keys (see python-dictionaries) — not its values, and not its key/value pairs. To reach a value you index the dictionary with the key the loop hands you:

>>> for key in table:            # for loop over keys
...     value = table[key]
>>> h = {"red": 1, "blue": 2, "green": 3}
>>> for key in h:
...     print(key)
red
blue
green
>>> for key in h:
...     print(h[key])
1
2
3

Exercise: character index

Write a function that maps each character of a string to the list of indices at which it occurs:

def char_index(xs: str) -> dict[str, list[int]]:
    """ Return a dictionary mapping the characters (length one
    substrings) of <xs> to the list of indices at which they occur.
    >>> char_index("")
    {}
    >>> char_index("aaAbbB")
    {'a': [0, 1], 'A': [2], 'b': [3, 4], 'B': [5]}
    """
acc = {}
for k, x in enumerate(xs):
    if x in acc:
        acc[x].append(k)
    else:
        acc[x] = [k]
return acc

Summary

For-loops let us iterate through anything iterable. Strictly speaking they’re not necessary (a while-loop, or copy-pasting code k times, can always do the same job) but they’re used often enough to merit their own dedicated syntax.

Next: 2025-08-25-comprehensions (Lecture 5B) — a rapid way of building lists and dictionaries that would otherwise need an accumulator for-loop.