# Need help with math for List and For Loops practice problem 15

Hello everybody, I got the answer question right, but in an inefficient way that would not at all help if I was using a large dataset. Screen Link: https://app.dataquest.io/m/1009/lists-and-for-loops-practice-problems/15/adding-all-matrix-entries

The question gave me a matrix. And then asked: “Write a Python program that sums all values in the matrix stored in variable `matrix` . Assign the result to a variable named `total` .” How I approached it was to take each row, and use a seperate for loop for it so that it could change a variable named total outside of the loop that was assigned the integer 0.

My Code was :
\\

total = 0
for values in matrix:
total = values + (total)

for values in matrix:
total = values + (total)

for values in matrix:
total = values + (total)

print (value_1)
\\

I got the answer right, but I didn’t understand the code that was given in the answers part. Can somebody explain the math to me in really simple terms, I am quite rusty on my math and im relearning as I go.

This was the code in the answer;
\\

matrix = [
[0, 9, 5, 4],
[8, 2, 3, 0],
[1, 5, 3, 2]
]

num_rows = len(matrix)
num_cols = len(matrix)

total = 0
for row in range(num_rows):
for col in range(num_cols):
total += matrix[row][col]
\\

Thanks in advance for the help!

1 Like

What you did was take each row in the matrix (as matrix, matrix and matrix) separately and add them to the variable ‘total’. What DQ did was run a loop through each row (num_rows=3) and add the numbers in that row to ‘total’ .
So in your case, all you had to do to make the code more efficient was to loop through the rows in ‘matrix’ like the code below -

``` for i in range(3): for values in matrix[i]: total = values + (total) ```

4 Likes

Thank you so much! I think that I understand, but I’d have to experiment more with code.

basically, the first for loops sets the parameter for the second for loop right? As in, if I had, say for example, 50 lists in a matrix. And I wanted to do data on 30-50.

I could do, for i in range (30,50)
for values in matrix [i]
total = values + (total)

Would that be correct? Even if I may not understand, I’ll keep coming back to your answer to try. Thank you so much for the help!

1 Like

@MohammedElnaiem

That is correct! You’ve got it!
Happy to be of help.

1 Like

Please, I have a problem to understand the answer code for this problem,
the answer code is (as mentioned above) is:
\

matrix = [
[0, 9, 5, 4],
[8, 2, 3, 0],
[1, 5, 3, 2]
]

num_rows = len(matrix)
num_cols = len(matrix)

total = 0
for row in range(num_rows):
for col in range(num_cols):
total += matrix[row][col]
\
how does the loop iterate through the lists values rather than iterates through the range values?
for the variable (num_rows), the range is 4, so the values of the variable (row) should be: 0,1,2,3, not the matrix lists values.
I’m confused, may anyone please guide me for this.

You are right. It does iterate through the range values, i.e, 0 to 2 for `num_rows` and 0 to 3 for `num_cols`. It accesses the matrix values when you index it to the `matrix` variable. So

matrix = 0
matrix = 9
matrix = 5
matrix = 4
matrix = 8

and so on. That is what is being added to the `total` variable.

Unless I am missing something, even the given answer is over-complicated.

``````total = 0

for row in matrix:  # loop through every row in our matrix
for col in row:   # loop through every column in a given row
total += col   # add the value from that row,col to our total
``````

With this version, we skip the unnecessary steps of checking the length and setting a range for the for loops. We don’t need to use the indexes at all; the for loops will go through every row and column by default.

3 Likes

It works as simple as that:

``````total = 0
for row in matrix:
total += sum(row)``````
5 Likes

I got to this same answer before I checked the DQ solution - isn’t this simpler and more efficient that the double for loop the DQ solution uses? Is there a broader good practice reason that you’d use two for loops instead of one here?

chef’s kiss, I knew there was a deadly simple solution in here somewhere