Pythonic way to print 2D list — Python

Pythonic way to print 2D list — Python

There are a lot of ways. Probably a str.join of a mapping of str.joins:

>>> a = [[1,2,3],
...          [4,5,6],
...          [7,8,9]]
>>> print(n.join(map(.join, a)))
123
456
789
>>>

Best way in my opinion would be to use print function. With print function you wont require any type of joining and conversion(if all the objects are not strings).

>>> a = [[1,2,3],
...      [4, 5, 6],   # Contains integers as well.
...      [7,8,9]]
...

>>> for x in a:
...     print(*x, sep=)
...
...
123
456
789

If youre on Python 2 then print function can be imported using from __future__ import print_function.

Pythonic way to print 2D list — Python

Like this:

import os

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
print(os.linesep.join(map(.join, array)))

Leave a Reply

Your email address will not be published.