Pythonic way to print a 2D list - Python

I have a 2D list of characters this way:

a = [['1','2','3'],
     ['4','5','6'],
     ['7','8','9']]

What is the most pythonic way to print a list as a whole block? That is, there are no commas or brackets:

123
456
789
+4
source share
4 answers

There are many ways. Probably a str.joinmapping str.joins:

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

Like this:

import os

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

Pythonic, :

print('\n'.join(''.join(i) for i in array))
0

, -, print. print ( ).

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

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

Python 2, from __future__ import print_function.

0

Source: https://habr.com/ru/post/1681171/


All Articles