Reputation: 891
I have the following list:
l = [[1,2,3],[4,5],[6]]
I can get the following result:
[1, 2, 3]
[4, 5]
[6]
by using this code:
for n in l:
print n
How can I get the following results which are not list:
1, 2, 3
4, 5
6
I am trying this:
for n in l:
for s in n:
print s
but it gives this:
1
2
3
4
5
6
Upvotes: 0
Views: 1498
Reputation: 113814
>>> print '\n'.join(str(n).strip('[]') for n in l)
1, 2, 3
4, 5
6
Upvotes: 1
Reputation: 368904
>>> l = [[1,2,3],[4,5],[6]]
>>> for ns in l:
... print ', '.join(map(str, ns))
...
1, 2, 3
4, 5
6
>>> for ns in l:
... print str(ns)[1:-1]
...
1, 2, 3
4, 5
6
Upvotes: 1
Reputation: 34146
With:
for n in l:
for s in n:
print s,
print
You will get:
1 2 3
4 5
6
Upvotes: 3