change all the numbers in a matrix

I have a matrix that is 242x322. what is the fastest way to change all the 7’s to 0’s.

here is the code I used to create my matrix

row = 242
collum = 322

g.grid = []
for i in range(row):
    g.grid.append([0] * collum)

You can initialize it faster like:


Anything wrong with this?

for row in mat:
    for i, cell in enumerate(row):
        if cell == 7:
            row[i] = 0
  • operator creates a shallow copy

all rows will share the same data

thank you. that works perfectly

operator creates a shallow copy

you’re right, never notices before… that sucks :frowning: