← Back to Nested Lists

Sum Each Row

2 of 6

Exercise 1: Sum Each Row

Given a grid of numbers, print the sum of each row.

grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

Expected output:

Row 0: 6
Row 1: 15
Row 2: 24

Starter Code

grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] for r in range(len(grid)): total = ___ for num in ___: total += ___ print(f"Row {r}: {total}")

Hints

Hint 1 — Initialize the total

Start each row's total at 0: total = 0

Hint 2 — What to loop over?

Loop over each number in the current row: for num in grid[r]:

Hint 3 — Adding up

Add each number to the total: total += num


Solution

Show Solution
grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] for r in range(len(grid)): total = 0 for num in grid[r]: total += num print(f"Row {r}: {total}")

How it works:

  • Outer loop goes through each row index (0, 1, 2)
  • For each row, reset total to 0
  • Inner loop adds up every number in that row
  • Print the row number and its sum
💻

Try it yourself

Code: Sum Each Row

Loading Python runtime…
Python
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

✅ Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

✅ Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit