[Python] Why can't I use nested for loops to change values in matrices? [duplicate]

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    I believe I'm missing something fundamental when it comes to for loops and/or matrices.

    def change_value(my_matrix: list, increase: int):
    for row in my_matrix:
    for column in row:
    column += increase
    return my_matrix

    matrix = [[1,2,3],[4,5,6],[7,8,9]]
    change_value(matrix, 3)


    In the code above, why doesn't column += increase actually increase the value of each item in the matrix?

    Using the debugger I can see that row and column both point at the correct values, and the value does increase, but it remains unchanged in the original matrix.

    The course I'm in explains the correct way to do this, I'm not looking for solutions, rather an explanation to the logic/inner workings of Python that lead to this behavior.

    Continue reading...

Compartilhe esta Página