6

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
asked Jul 11, 2017 at 7:07

5 Answers 5

11

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
>>>
answered Jul 11, 2017 at 7:09
Sign up to request clarification or add additional context in comments.

12 Comments

Oh right! Arrays, lists and arraylists... always screwing me up!
Always here to try to improve an already good answer :) could be worth a bench to compare join(map('' vs join([''.join(x) for x in a]). In the latter, the outer join knows the size of the list and the items and allocates the output string in one go.
@Jean-FrançoisFabre yeah, if performance really were an issue, probably materializing into a list will be faster. Likely, this code won't be performance critical. From the point of view of style and ease, I would be OK with this.
compiled map makes up for the speed loss of the listcomp. A generator comprehension would be the slowest.
If memory isn't a concern, the either the above or maybe print('\n'.join(list(map(''.join, a)))) if you are on Python 3. In any event, you can just benchmark with a couple large lists using the timeit module. Indeed, the "Basic examples" in the docs are very similar to your situation...
|
3

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.

answered Jul 11, 2017 at 16:54

Comments

1

I'm a beginner learner, so please take my post with a few spoons of salt. But I have a better idea ( which includes printing a list with string+ integers)

D2List=[[1,2,3],[4,"Bingo",5],[6,7,8]]
for x,y,z in D2List:
 print ("x,y,z")

I got it working: https://github.com/Bahaa2468/Python/blob/22e6cfc9b478f9336ef0be283e2693c40f13d538/Bingo%20Game.Generator.py

answered Feb 11 at 16:46

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Like this:

import os
array = [['1','2','3'],
 ['4','5','6'],
 ['7','8','9']]
print(os.linesep.join(map(''.join, array)))
answered Jul 11, 2017 at 7:11

Comments

0

If you're looking for Pythonic then you surely need a generator comprehension:

print('\n'.join(''.join(i) for i in array))
answered Jul 11, 2017 at 7:16

3 Comments

in the case of join, passing a list comprehension instead is faster because join creates one anyway (needs that to pre-compute the size): '\n'.join([''.join(i) for i in array]) even if it's uglier :)
@Jean-FrançoisFabre Yup, here's where the conversion happens.
Thanks for that @Jean-FrançoisFabre, I didn't know that. OTOH, the OP didn't ask for the fastest method. ;-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.