Search code examples
pythonnested-lists

How do i print nested list with new row on each nested list within the main list


Currently coding a python sudoku game and I've got a nested list acting as my board, however the list when printed prints out all in one single line, how do I get each seperate list within the nested list to print out in a new row of its own?

code below if needed

#board initilization
board = [
 [0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 2, 0, 0, 0, 0, 0, 0, 2],
 [0, 0, 0, 3, 0, 3, 0, 0, 0],
 [0, 0, 5, 0, 0, 0, 0, 0, 0],
 [0, 8, 0, 0, 0, 6, 0, 5, 0],
 [0, 0, 0, 0, 0, 0, 0, 0, 0],
 [0, 0, 0, 2, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0, 0, 9, 0],
 [0, 0, 0, 1, 0, 0, 0, 0, 0]
]


print(board)

Just tried to print it normally and it printed all on a single line until the line was used


Solution

  • print will not add /n, you will need to either add it specifically or just do a loop of rows and print 1 time per row

    for row in board:
      print(row)
    
    [0, 0, 0, 0, 0, 0, 0, 0, 0]
    [0, 2, 0, 0, 0, 0, 0, 0, 2]
    [0, 0, 0, 3, 0, 3, 0, 0, 0]
    [0, 0, 5, 0, 0, 0, 0, 0, 0]
    [0, 8, 0, 0, 0, 6, 0, 5, 0]
    [0, 0, 0, 0, 0, 0, 0, 0, 0]
    [0, 0, 0, 2, 0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0, 0, 0, 9, 0]
    [0, 0, 0, 1, 0, 0, 0, 0, 0]