Nested Loop Multiplication Grid

Medium ⏱ 12 min 51% acceptance ★★★★☆ 4.4
Write a function multiplication_grid(rows, cols) that returns a 2D list (list of lists) where grid[i][j] = (i + 1) * (j + 1) for i in range(rows) and j in range(cols), built with nested for loops.

Examples

Example 1
Input
multiplication_grid(2, 3)
Output
[[1, 2, 3], [2, 4, 6]]
Explanation

Row 0 is 1*(1,2,3); row 1 is 2*(1,2,3).

Constraints

  • rows and cols are positive integers.

Topics

Loopsnested loops

Companies

MicrosoftAdobe

Hints

Hint 1

Outer loop builds each row; inner loop fills in that row's columns.

Hint 2

Append each computed value to a row list, then append the row list to the grid.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.