Outside Resources:
Blog post – nested loops with lists and if statements
Loops can be inside other loops.
Nesting is when a code structure is inside another structure.
An example of nested loops is below. It has an inner loop that’s inside of an outer loop.
print('x | y')
for x in range(3): #outer loop
print('-----')
for y in range(5): #inner loop
print(x,"|", y)
print()
print("All Done")
The program’s output is shown below.
The outer loop repeats three times. The inner loop repeats five times for each iteration of the outer loop, for a total of 15 repeats.
Notice that the line, print('-----') only executes once for each outer loop, before the inner loop begins. It makes lines at the top of each set of y values. Similarly, the print() command (creating a blank line after each set of y values) also only executes once per outer loop, after the inner loop has finished.
Output:
x | y ----- 0 | 0 0 | 1 0 | 2 0 | 3 0 | 4 ----- 1 | 0 1 | 1 1 | 2 1 | 3 1 | 4 ----- 2 | 0 2 | 1 2 | 2 2 | 3 2 | 4 All Done
Notes:
- See how the value of
xstays at zero until the entireyloop (inner loop) completes a cycle from 0 to 4. Only then doesxincrease to one, and theycycle starts over again. - The inner and outer variables must use different variables.
- The loops could be for loops or while loops.
- Pay attention to indents, because this determines if a command is in the inner loop, outer loop, or neither.
- If a line is indented twice, it is inside the inner loop (or inside both loops, if you like).
- The number of times the inner loops code block executes is equal to the outer loop iterations multiplied by the inner loop iterations.
- In this example, the inner loop executes 3 x 5 = 15 times.
Nested Loops with a List of Lists.
Here’s another example where we can use nested loops to iterate through a nested data structure. That is, we loop through lists that are inside of other lists.
In the nested structure below, the outer list contains several smaller lists that each contain three quiz scores each.
Tasks:
a) Print each student’s average score (average of each list)
b) Print the overall class average
scores = [
[80, 95, 72],
[99, 95, 92],
[75, 80, 82],
[70, 80, 95]
]
overall_total = 0
for student_list in scores:
student_total = 0
for quiz in student_list:
student_total += quiz
overall_total += student_total
avg = student_total / 3
print(avg)
num_of_scores = len(scores) * 3
class_avg = overall_total / num_of_scores
print(class_avg)
Notes:
Pay attention to the differences in the way we calculate the calculating the single student averages compared to class average.
Pay special attention to the two for loop lines.
# outer loop for student_list in scores:
We’re creating the variable student_list to represent one list of three values at a time.
In the inner loop, we iterate through that list of three values:
# inner loop
for quiz in student_list:
The variable quiz represents a single number from the list student_list.
Notice how the new variable from the outer loop becomes the list that we’re looping through in the inner loop, and this list changes each time we restart the inner loop.