print two dimensional list

inetphantom picture inetphantom · Oct 31, 2013 · Viewed 19.2k times · Source

I have a list, in which is another list and I want to doc.write(a)

a = [[1, 2, "hello"],
     [3, 5, "hi There"],
     [5,7,"I don't know"]]
doc.write(''.join(a))



TypeError: sequence item 0: expected str instance, list found

How can I handle this, do I have to make a for-loop in which I join and add all the sublists?

The real goal was to make it somehow readable for human beeing, but I didn't wanted a finished solution from you.

Answer

arshajii picture arshajii · Oct 31, 2013

You can try something like

>>> a = [[1, 2, "hello"],[3, 5, "hi There"],[5,7,"I don't know"]]
>>> 
>>> ''.join(str(r) for v in a for r in v)
"12hello35hi There57I don't know"

i.e.

doc.write(''.join(str(r) for v in a for r in v))