Pythonic way to print 2D list -- Python

13,072

Solution 1

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
>>>

Solution 2

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.

Share:
13,072
Graviton
Author by

Graviton

For inquiries: email [email protected]

Updated on June 08, 2022

Comments

  • Graviton
    Graviton almost 2 years

    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
    
  • Graviton
    Graviton almost 7 years
    Oh right! Arrays, lists and arraylists... always screwing me up!
  • Jean-François Fabre
    Jean-François Fabre almost 7 years
    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.
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 7 years
    @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.
  • Jean-François Fabre
    Jean-François Fabre almost 7 years
    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çois Fabre
    Jean-François Fabre almost 7 years
    compiled map makes up for the speed loss of the listcomp. A generator comprehension would be the slowest.
  • Christian Dean
    Christian Dean almost 7 years
    @Jean-FrançoisFabre Yup, here's where the conversion happens.
  • Graviton
    Graviton almost 7 years
    @juanpa.arrivillaga Currently performance is priority, as the 2d list is pretty massive. What would be the most efficient way be in this case?
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 7 years
    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...
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 7 years
    And, if performance is really critical, you may consider using a bytearray. This is a mutable version of a str in Python 3, allowing for fast-conversion between a list-like mutable data-structure and a string...
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 7 years
    @Graviton maybe a list of bytearrays would be the best... anyway, you can benchmark it!
  • Graviton
    Graviton almost 7 years
    Great! I'll look into your suggestions... Off to to some benchmarking.
  • Graviton
    Graviton almost 7 years
    @juanpa.arrivillaga Last question: How would I efficiently print a list of bytearrays without a generator to to decode it?
  • juanpa.arrivillaga
    juanpa.arrivillaga almost 7 years
    @Graviton actually, I didn't consider the bytes-to-string conversion overhead, probably not a good idea anyway.
  • Turn
    Turn over 6 years
    Thanks for that @Jean-FrançoisFabre, I didn't know that. OTOH, the OP didn't ask for the fastest method. ;-)
  • Anonymous
    Anonymous over 4 years
    Note that this won't work if the elements of a aren't strings, unlike a normal print(one_d_list). For an answer that works even when the elements aren't strings, use for x in a: print(*x, sep='')