Start learning free

LESSON 255 OF 673 · NESTED LOOPS OVER GRIDS

Row Sums in Python: Collecting a Sum for Each Row

Practice Read the full explanation

Given a grid of integers grid, return a list with the sum of each row, in the same order as the rows.

A grid is a table of numbers made of rows. Every row has the same number of values, one in each column. Rows are counted from the top and columns from the left, both starting at 0.

 

Example 1:

Input: grid = [[1, 2, 3],
               [4, 5, 6]]
Output: [6, 15]
Explanation: Row 0 adds up to 1 + 2 + 3 = 6 and row 1 to 4 + 5 + 6 = 15.

Example 2:

Input: grid = [[-1, -2],
               [-3, -4],
               [5, 5]]
Output: [-3, -7, 10]
Explanation: One sum per row, top to bottom. A row of negative numbers has a negative sum.

Example 3:

Input: grid = [[3],
               [1],
               [4]]
Output: [3, 1, 4]
Explanation: Every row has a single value, so each row's sum is that value.

 

Constraints:

  • grid has at least 1 row and at most 10 rows.
  • Every row has the same number of values, at least 1 and at most 10.
  • Every value is an integer between -1000 and 1000.
STEP 1 OF 7
    Ctrl + Enter to check

    PYTHON · NESTED LOOPS OVER GRIDS

    Row Sums in Python, explained

    Problem overview

    The method rowSums receives grid, a list of rows where each row is a list of integers, and returns a list with the sum of each row, in the same order as the rows. Every row has the same number of values.

    For [[1, 2, 3], [4, 5, 6]] the result is [6, 15]: row 0 adds up to 6 and row 1 to 15. For [[-1, -2], [-3, -4], [5, 5]] the result is [-3, -7, 10], one sum per row, top to bottom. For [[3], [1], [4]] each row has one value, so the result is [3, 1, 4].

    Approach

    This is the grid-sum idea with one change: the sum is not one number for the whole grid but one number per row. So the running total must start again at 0 for every row, and each finished total is saved in a result list before the next row begins.

    That fixes the shape of the code. The outer loop visits rows. Inside it, a fresh total starts at 0, an inner loop adds up the row's values, and when the inner loop ends, the total is appended to the result.

    Step by step

    1. Start an empty list named sums. It collects one sum per row.
    2. Loop row over grid. The loop runs once per row, so it produces one sum per row.
    3. Create total = 0 inside the loop. Because it sits inside, it restarts for every row, and one row's numbers never mix into the next row's sum.
    4. Loop value over row inside the outer loop. It visits only the current row.
    5. Add value to total in the inner loop.
    6. Append total to sums after the inner loop, still inside the outer one. At that point the whole row has been added, and appending at the end of each pass keeps the sums in row order.
    7. Return sums after the outer loop.

    The solution in Python

    class Solution:
        def rowSums(self, grid: List[List[int]]) -> List[int]:
            sums = []
            for row in grid:
                total = 0
                for value in row:
                    total += value
                sums.append(total)
            return sums
    

    Python details:

    • Indentation decides which loop a line belongs to. sums.append(total) lines up with the inner for, so it runs once per row. Indented one level deeper, it would run once per value.
    • append changes the list in place and returns None, so don't assign its result back to sums.
    • The name sums is free to use. Python has a built-in sum, but sums is a different name and hides nothing.

    The built-in gives a shorter version: [sum(r) for r in grid] returns [6, 15] for the first example. The explicit loops show where each number comes from.

    Edge cases and common mistakes

    • Creating total before the outer loop. Then it never resets, and each sum includes all earlier rows. For the first example the result would be [6, 21] instead of [6, 15].
    • Appending inside the inner loop. That adds a partial sum after every value, so the list grows by one item per cell instead of one per row.
    • Returning inside the outer loop. The result is a list holding only the first row's sum, [6] for the first example.
    • Rows with one value. They need no special case: the inner loop runs once and the sum is that value.
    • Negative values. They work normally, and a row of negatives gives a negative sum.

    Testing

    • [[1, 2, 3], [4, 5, 6]] gives [6, 15].
    • [[-1, -2], [-3, -4], [5, 5]] gives [-3, -7, 10].
    • [[3], [1], [4]] gives [3, 1, 4].
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free