-4

I am using the below code to prepare a matrix of m×n, which should contain integers in a given range with randomness. I am unable to trace the operation of list.clear() which makes so that all rows of my matrix to be identical. If I assign a new list instead of list.clear() it works fine. I will post the code and output below. What is the reason for the behavior I am witnessing using clear()?

code

import random

    m = 5  # number of rows
    n = 6  # number of columns
    mat = []  # a list for making the rows of matrix
    matrix = []
    for i in range(m):
        mat.clear()  # works fine if I use mat=[]
        print(mat)
            for i in range(n):
                mat.append(random.randint(0, 2))

        print(mat)
        matrix.append(mat)
        print(matrix)

output using clear(): all rows of the matrix are identical

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

    Process finished with exit code 0

output using mat=[]

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

    Process finished with exit code 0
m.piras
  • 345
  • 1
  • 2
  • 19
  • 3
    Duplicate of [How is list.clear() different from list = \[\]?](https://stackoverflow.com/questions/51116967/how-is-list-clear-different-from-list) – Guy Incognito Feb 21 '23 at 10:02
  • `[]` creates a new list, so anything referencing old one stays the same. `.clear()` actually removes all the items from existing list, keeping the reference the same. – matszwecja Feb 21 '23 at 10:14
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 21 '23 at 12:41

1 Answers1

0

The main difference is that mat.clear() removes the content of the list without changing its binding to the variable mat. It's the same list, but empty. Instead, mat = [] creates a brand new, empty list and binds it to the variable mat. It's a completely different list. Now, your variable matrix is referencing the list mat multiple times. Using clear() and replacing the values inside the mat variables, you are replacing its content in every place where you appended mat. Let's take you first two iterations where you use clear() as an example:

[]                                       # mat is empty
[1, 1, 2, 0, 1, 0]                       # mat contains 1, 1, 2, 0, 1, 0
[[1, 1, 2, 0, 1, 0]]                     # mat is appended to matrix
[]                                       # mat is emptied, but still referenced by matrix, that would now be [[]]
[2, 0, 2, 0, 2, 1]                       # mat is filled with 2, 0, 2, 0, 2, 1 and so every reference to mat will show this content from now on, until the next clear()
[[2, 0, 2, 0, 2, 1], [2, 0, 2, 0, 2, 1]] # matrix references two times the same list, with the same content

The difference will be more evident if you print matrix immediately after your clear() or your mat = []

m.piras
  • 345
  • 1
  • 2
  • 19