Nested for
loops
There are many cases when you want to have iteration happen within an iteration. In this case, we will “nest” our loops–put one loop inside of another loop.
Notice that your inner for
loop is entirely indented within the outer for
loop.
Each time the outer loop iterates in this example (total of 10 times), the inner loop will iterate 5 times.
Notice that the inner loop “starts over” each time the outer loop iterates.
We can comupute the total number of times the inner loop iterates with the following formula:
Nested for
loops with an accumulator
Recall the accumulator pattern from the previous section. Any for loop you have (whether it is an outer loop or an inner loop) can use the accumulator pattern.
In the above example, the variable s
will be reset each time the outer loop iterates.
We could have produced the same output by using string multiplication in this instance:
For some problems, we can’t though. Imagine that we want to write a function that prints output like the following:
In this function we want to be able to vary how many rows there are and how many columns there are.
# TODO: run me. Call the function!
# Notice that the step #2 for the accumulator variable
# uses the value column (the value that the inner loop is iterating over)
def numbers_string_box(rows, columns):
for row in range(rows):
s = "" # 1) declare accumulator variable
for column in range(3):
s += str(column) # 2) update variable
print(s) # print result
Now, what if we want to change our function from above to a function that prints output like the following:
Notice that each number in this box is equal to row + column
:
# TODO: run me. Call the function!
# Notice that the step #2 for the accumulator variable
# uses BOTH the value column AND the value row
def numbers_string_box(rows, columns):
for row in range(rows):
s = "" # 1) declare accumulator variable
for column in range(3):
s += str(row + column) # 2) update variable
print(s) # print result