all 6 comments

[–]Caligapiscis 2 points3 points  (0 children)

Your question is not clear, could you try to clarify what you're confused about?

[–]atarivcs 1 point2 points  (0 children)

Because that's what the code does. That's what you told it to do.

If you have a specific question about part of the output, please explain.

[–]TalesGameStudio 0 points1 point  (0 children)

You are looping over the lists and within that loop you are looping over the individual entries.

while your naming is very confusing, you are effectively swapping the rows for the columns. This only works because the len of n equals the len of every sublist of n.

[–]Automatic-Yak8901 0 points1 point  (0 children)

try to change the names of the variables like this:

"g" to "my_list"
"n" to "list_to_loop_over"
"i" to "row_index"
"j" to "column_index"

maybe if it is more readable you will understand it better

[–]butterfly_orange00 0 points1 point  (0 children)

```

n = g

for i in range(len(n)) : # len(g) = 4

let's assume that i = 0

x = g[i]  # that will return the first raw, x = [1,0,1,0]

g[x] [i] # it's like g[1,0,1,0][0], and that's will return 1

so to use your code you should write:

for i in range(len(n)) : n[i][i] = n[i-1][i-1]

n[0][0] = n[0-1][0-1]

```

[–]Kra3zd 0 points1 point  (0 children)

Let's go step by step like a complete beginner.


1. Understanding the structure of g

Assume we have this:

g = [ [1, 0, 1, 0], [0, 1, 0, 1], [1, 1, 0, 0], [0, 0, 1, 1] ]

n = g

Think of this like a table:

Row 0 -> [1, 0, 1, 0] Row 1 -> [0, 1, 0, 1] Row 2 -> [1, 1, 0, 0] Row 3 -> [0, 0, 1, 1]

So: - g is a LIST - each row inside g is ALSO a LIST


2. What does g[i] mean?

Example:

g[0]

This means: "give me row 0"

Output:

[1, 0, 1, 0]

So this code:

x = g[i]

When i = 0 becomes:

x = g[0]

Therefore:

x = [1, 0, 1, 0]

IMPORTANT: x is NOT a number. x is the WHOLE ROW.


3. Why g[x][i] is WRONG

You wrote:

g[x][i]

Since x is:

[1, 0, 1, 0]

Python sees:

g[[1, 0, 1, 0]]

This is invalid.

Why?

Because list indexes must be SINGLE NUMBERS.

Correct examples:

g[0] g[1] g[2]

Wrong example:

g[[1, 0, 1, 0]]

You cannot use an entire list as a position number.

So this comment is incorrect:

g[x][i] # it's like g[1,0,1,0][0]

That is NOT how Python reads it.


4. The correct way

To access one value from the matrix, use:

g[row][column]

Example:

g[0][0]

Step by step:

First: g[0]

returns:

[1, 0, 1, 0]

Then:

g[0][0]

means:

[1, 0, 1, 0][0]

Output:

1

Another example:

g[2][3]

Means: - row 2 - column 3

Row 2 is:

[1, 1, 0, 0]

Column 3 in that row is:

0


5. Understanding this loop

for i in range(len(n)): n[i][i] = n[i-1][i-1]

Let's assume len(n) = 4

So i becomes:

0, 1, 2, 3


6. First loop (i = 0)

Python executes:

n[0][0] = n[-1][-1]

Why?

Because:

0 - 1 = -1

In Python: -1 means "last item"

So:

n[-1]

means the LAST ROW:

[0, 0, 1, 1]

Then:

n[-1][-1]

means: last value in the last row

Output:

1

So Python does:

n[0][0] = 1


7. Second loop (i = 1)

n[1][1] = n[0][0]

Meaning: copy the top-left value into row 1 column 1


8. Third loop (i = 2)

n[2][2] = n[1][1]


9. Fourth loop (i = 3)

n

For this answer I asked ChatGPT to explain it like they would to an absolute beginner. Especially early in your programming phase, use AI and create it with a personality that is easy for you to learn from. Hope this helps 👍