DM23545
DM23545

Reputation: 33

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

Upvotes: 0

Views: 41

Answers (2)

Vijay A Verma
Vijay A Verma

Reputation: 19

A simple way would be

print(*board, sep = "\n")

[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]

Upvotes: 0

Rauuuñ
Rauuuñ

Reputation: 48

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]

Upvotes: 1

Related Questions