Question

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
Was it helpful?

Solution

With:

for n in l:
    for s in n:
        print s,
    print

You will get:

1 2 3
4 5
6

OTHER TIPS

>>> 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
>>> print '\n'.join(str(n).strip('[]') for n in l)
1, 2, 3
4, 5
6
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top