Initializing a fixed length 2d list and populating without external library in python

12 Views Asked by At

I am trying to initialize an n by n list and then populate it by iterating through each row and column, but it doesn't work because say n = 2, [[None, None], [None, None]] does not get populated the same way as [[None] * 2] * 2.

For example:

n = 2 # int(input())
var_array = [[None] * n] * n
for i in range(n):
    for j in range(n):
        var_array[i][j] = (i, j)
        for v in var_array:
            print(v)
print(var_array)

Output: [[(1, 0), (1, 1)], [(1, 0), (1, 1)]] And that is because it populates both rows at col j instead of only row i:

>>> [[None, None], [None, None]] == [[None] * 2] * 2
True
>>> var1 = [[None, None], [None, None]]
>>> var2 = [[None] * 2] * 2
>>> var1[0][0] = 5
>>> var1
[[5, None], [None, None]]
>>> var2[0][0] = 5
>>> var2
[[5, None], [5, None]]

p.s. I am aware of better ways of populating an n by n list, but I need to do it this way - populate an already initialized 2d n by n list - to optimize a part of another program

0

There are 0 best solutions below