Reputation: 384
Assuming I have
a = [[0, 1], [0, 2]]
I want to, using a oneliner with join
and map
, print it as :
0 1
0 2
The closest I got is :
print("\n".join(map("".join, map(str, a))))
which gives :
[0, 1]
[0, 2]
Upvotes: 0
Views: 936
Reputation: 30268
You are one level of iteration away:
In []:
print('\n'.join(' '.join(map(str, b)) for b in a))
Out[]:
0 1
0 2
Or:
In []:
print('\n'.join(map(' '.join, map(lambda b: map(str, b), a))))
Out[]:
0 1
0 2
Or if you really don't want to use lambda, then you can use functools.partial
but now it is getting really ugly:
import functools as ft
In []:
print('\n'.join(map(' '.join, map(ft.partial(map, str), a))))
Out[]:
0 1
0 2
Upvotes: 1
Reputation: 33087
You can do this much more cleanly by using a for-loop with print
instead of a one-liner with map
and join
:
for x in a:
print(*x)
print
automatically converts its arguments to string, joins them on spaces, and adds a trailing newline.
Upvotes: 0
Reputation: 59305
You can do it using map
and lambda
:
print("\n".join(map(lambda x: " ".join(map(str, x)), a)))
0 1
0 2
Edit: Just saw the comment with the same method, oh well.
Upvotes: 0