Reputation: 827
Bloody stupid question from dead brain...
I have a list:
[1,2,3,4,5,6,7,8,9]
which I sliced up into 3 lists:
splits = [1,2,3],[4,5,6],[7,8,9]
which I would like to now have printed on individual lines such that
print splits
gives
[1,2,3]
[4,5,6]
[7,8,9]
Can someone please 1) whack me upside the head and 2) remind me how to do this?
Upvotes: 1
Views: 82
Reputation: 143027
If
s = [[1,2,3],[4,5,6],[7,8,9]] # list of lists
or
s = [1,2,3],[4,5,6],[7,8,9] # a tuple of lists
then
for i in s:
print(i)
will result in:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Guided by the Zen of Python: Simple is better than complex.
Upvotes: 6
Reputation: 6769
Is the 3 lists a list of lists? ex [[1],[2],[3]]
?
if so, just:
for sliced_list in list_of_lists:
print(sliced_list)
With your given syntax [1,2,3],[4,5,6],[7,8,9]
, it is a tuple of lists , which will behave the same when using the for statement.
Upvotes: 2
Reputation: 3555
I do not understand your first quesion.
For the second one, you might like to do it like:
>>> splits = [1,2,3],[4,5,6],[7,8,9]
>>> print "\n".join([repr(item) for item in splits])
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
Upvotes: 0
Reputation:
Use the string join function:
print '\n'.join(str(x) for x in [[1,2,3],[4,5,6],[7,8,9]])
Upvotes: 0