0
def main():
    row = 5
    col = 3
    count = 0
    board = list([[""]*col]*row)
    for i in range(0,row):
        for n in range(0,col):
            board[i][n] = str(count)
            print board[i][n] , count, i, n
            count+=1

    print board
main()

When I run this code, I expect my list to be :

[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14]]

and when I print the individual indices inside the loop, it assigns properly but after exiting the loop, I fine the output ends up being

[[12,13,14],[12,13,14],[12,13,14],[12,13,14],[12,13,14]]
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Dee
  • 47
  • 1

1 Answers1

0

Try replacing this line

board = list([[""]*col]*row)

with

board = [[0 for x in range(col)] for y in range(row)]

This change makes the code work for me.

pgngp
  • 1,552
  • 5
  • 16
  • 26