Andrey Dobrikov
Andrey Dobrikov

Reputation: 457

2D array of 2 cells lists

I'm trying to implement a minesweeper so I have a 2d array, where each cell holds ['H', ' '].

When I try to update the second value in a selected rowXcol it updates all values in the array and not only the selected. i.e:

  0 1 2 3
0        
1        
2        
3         
myArr[1][2][1] = 'x'
  0 1 2 3
0 x x x x
1 x x x x 
2 x x x x
3 x x x x

instead of :

  0 1 2 3
0        
1     x
2
3

Upvotes: 1

Views: 126

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58965

If you create your 2D nested list like this:

lines = 4
cols  = 4
a = [[['H',' '] for j in range(cols)] for i in range(lines)]

You will not have this problem.

Upvotes: 1

Related Questions