Reputation: 123
How can I split a list of lists per lines?
list = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
into:
a b c
d e f
g h i
Upvotes: 2
Views: 5529
Reputation: 36474
myList = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
for subList in myList:
print " ".join(subList)
(Note -- don't use reserved words like list
or str
to name your variables. This will bite you sooner rather than later)
Upvotes: 2
Reputation: 500207
In [11]: lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
In [12]: print('\n'.join(' '.join(l) for l in lst))
a b c
d e f
g h i
Upvotes: 7
Reputation: 287755
You don't want to split the elements, you want to join them:
>>> lst = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
>>> print('\n'.join(' '.join(sublist) for sublist in lst))
a b c
d e f
g h i
Note that list
is a terrible name for a variable since it overshadows the built-in list
. Therefore, I renamed the variable to lst
.
Upvotes: 5
Reputation: 15310
In [1]: mylist = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
In [2]: for item in mylist:
...: print ' '.join(item)
a b c
d e f
g h i
Upvotes: 5