List and Loop: #13 [problem understanding the range() function in a matrix]

Screen Link:

My Code:

this is the solution provided: 

matrix_of_ones = []
for _ in range(7):
    matrix_of_ones.append([1, 1, 1])

What I expected to happen:
After checking the solution provided, I expected to see a list of lists in which the first element of each list would be a number from 0-6. This is because I used the range(7) function. The range(7) function should create an output like the one below:

0
1
2
3
4
5
6

so, why didn’t each appended list inside matrix_of_ones resulted as:
[[0, [1,1,1]],
[1, [1,1,1]],
[2, [1,1,1]]…until
[6, [1,1,1]]]

You don’t see the numbers from 0 to 6 because you don’t append them the matrix_of_ones list. In each interaction, you only append [1, 1, 1].

If you want to have this number appended to the list, you can do something like this:

matrix_of_ones = []
for _ in range(7):
    matrix_of_ones.append([_,[1, 1, 1]])

And then the output would be:

[[0, [1, 1, 1]],
 [1, [1, 1, 1]],
 [2, [1, 1, 1]],
 [3, [1, 1, 1]],
 [4, [1, 1, 1]],
 [5, [1, 1, 1]],
 [6, [1, 1, 1]]]

Perfect. Got it.

Thank you.

1 Like