Stefan
Stefan

Reputation: 13

Python remove commas and apostrophes from list in output

I wanted to programm chess for the console.

To do that, I created a two dimensional array:

chessboard= [["a1","a2","a3","a4","a5","a6","a7","a8"],["b1","b2","b3","b4","b5","b6","b7","b8"],["c1","c2","c3","c4","c5","c6","c7","c8"],
["d1","d2","d3","d4","d5","d6","d7","d8"],["e1","e2","e3","e4","e5","e6","e7","e8"],["f1","f2","f3","f4","f5","f6","f7","f8"],
["g1","g2","g3","g4","g5","g6","g7","g8"],["h1","h2","h3","h4","h5","h6","h7","h8"]]

I filled it with different and my problem is the output:

-['1', '2', '3', '4', '5', '6', '7', '8']-
1['♖', '♘', '♗', '♔', '♕', '♗', '♘', '♖']1
2['♙', '♙', '♙', '♙', '♙', '♙', '♙', '♙']2
3['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']3
4['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜']4
5['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']5
6['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜']6
7['♟', '♟', '♟', '♟', '♟', '♟', '♟', '♟']7
8['♜', '♞', '♝', '♚', '♛', '♝', '♞', '♜']8
-['1', '2', '3', '4', '5', '6', '7', '8']-

Without the commas and apostrophes the output would look perfectly fine. But can I remove it or are there some other ways?

EDIT: Thanks to you my chessboards looks now pretty good :)

enter image description here

Upvotes: 1

Views: 501

Answers (2)

Ironkey
Ironkey

Reputation: 2607

Since you have nested lists you have to iterate over each of the sub lists to remove them from list format

for i in chessboard:
    for x in i:
        print(x, end  = " ")
    print("")
a1 a2 a3 a4 a5 a6 a7 a8 
b1 b2 b3 b4 b5 b6 b7 b8 
c1 c2 c3 c4 c5 c6 c7 c8 
d1 d2 d3 d4 d5 d6 d7 d8 
e1 e2 e3 e4 e5 e6 e7 e8 
f1 f2 f3 f4 f5 f6 f7 f8 
g1 g2 g3 g4 g5 g6 g7 g8 
h1 h2 h3 h4 h5 h6 h7 h8

and the pythonic way to do that:

for i in chessboard: print(*i) 

Upvotes: 1

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

You can just print and unpack:

for lst in chessboard:
    print(*lst)
a1 a2 a3 a4 a5 a6 a7 a8
b1 b2 b3 b4 b5 b6 b7 b8
c1 c2 c3 c4 c5 c6 c7 c8
d1 d2 d3 d4 d5 d6 d7 d8
e1 e2 e3 e4 e5 e6 e7 e8
f1 f2 f3 f4 f5 f6 f7 f8
g1 g2 g3 g4 g5 g6 g7 g8
h1 h2 h3 h4 h5 h6 h7 h8

Upvotes: 0

Related Questions