0

I have a 2D list:

boardlist = [[81] * cols] * rows

I want to append 1 to the elements in each column that are in the first two rows. Then a 0 to the elements in each column that are in the bottom two rows. Then a -1 everywhere else. So in an 8x8 boardlist it would look like this:

1   1  1  1  1  1  1  1
1   1  1  1  1  1  1  1 
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 
0   0  0  0  0  0  0  0
0   0  0  0  0  0  0  0 

What I've tried:

for row in boardlist:
    if boardlist.index(row) < 2:
        boardlist[row].append(1)
    elif boardlist.index(row) in range(len(boardlist) - 2, len(boardlist)):
        boardlist[row].append(0)
    else:
        boardlist[row].append(-1)

But my result is [[], [], [], [], [], [], [], [], [], []] What am I doing wrong?

LoopGod
  • 67
  • 5
  • `boardlist = [[81 for _ in range(cols)] for _ in range(rows)]` or eventually `boardlist = [[81]*cols for _ in range(rows)]` as integers are immutable – mozway Oct 17 '22 at 18:41

0 Answers0