I have a 2D list of characters in this fashion:
a = [['1','2','3'],
['4','5','6'],
['7','8','9']]
What's the most pythonic way to print the list as a whole block? I.e. no commas or brackets:
123
456
789
Traversing in a 2D array in python can be done by using a for loop. We can iterate through the outer array first and then at each element of the outer array, we have another array which is our inner array containing the elements. So for each inner array, we run a loop to traverse its elements.
Pass a list as an input to the print() function in Python. Use the asterisk operator * in front of the list to “unpack” the list into the print function. Use the sep argument to define how to separate two list elements visually.
There are a lot of ways. Probably a str.join
of a mapping of str.join
s:
>>> 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 won't 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 you're on Python 2 then print function can be imported using from __future__ import print_function
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With