Skip to content Skip to sidebar Skip to footer

Displaying Python 2d List Without Commas, Brackets, Etc. And Newline After Every Row

I'm trying to display a python 2D list without the commas, brackets, etc., and I'd like to display a new line after every 'row' of the list is over. This is my attempt at doing so:

Solution 1:

Simple way:

for row in list2D:
    print" ".join(map(str,row))

Solution 2:

Maybe join is appropriate for you:

print "\n".join(" ".join(str(el) for el inrow) forrowin ogm)

0000000000110100000011100000000110000011000000111000011011110011001111010000011000000011001011110000

Solution 3:

print"\n".join(" ".join(map(str, line)) for line in ogm)

If you want the rows and columns transposed

print"\n".join(" ".join(map(str, line)) for line inzip(*ogm))

Solution 4:

forrowin list2D:
    print(*row)

Solution 5:

To make the display even more readable you can use tabs or fill the cells with spaces to align the columns.

defprintMatrix(matrix):
    for lst in matrix:
        for element in lst:
            print(element, end="\t")
        print("")

It will display

6       8       99
999     7       99
3       7       99

instead of

6 8 99
999 7 99
3 7 99

Post a Comment for "Displaying Python 2d List Without Commas, Brackets, Etc. And Newline After Every Row"