Magic Magic - 3 months ago 12
Python Question

What's the shortest way of converting list of lists to a string, one inner list per line?

I have a list of lists and I'm looking for the shortest way of converting that data to a string where every inner list appears on a new line.

Assuming my input list is:

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


I want my output to be a string (let's call it l_2d_str), so that if i
print l_2d_str
, I get:

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

Answer

How about this?

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

l_2d_str = '\n'.join(map(repr, l_2d))
Comments