Editor

Sum All Grid Values

Intermediate · Write the code · from Nested Loops: Patterns, Performance, and When to Avoid Them

From the lesson: Nested Loops: Patterns, Performance, and When to Avoid Them

Think about a spreadsheet. It has rows and columns. To read every cell, you go through each row, and for each row, you go through each column. That's two loops - one inside the other. That's a nested loop.

Read the lesson

A grid of numbers is given. Use nested loops to calculate and print the sum of all values in the grid.

Expected output:

45
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Calculate the sum of all values

More Nested Loops exercises

All Python exercises · 50 practice problems · Control Flow quiz