Printing n elements per line in a list

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Tue Aug 15 23:59:28 EDT 2006


On Tue, 15 Aug 2006 19:49:17 -0700, John Machin wrote:

> Ugliness is in the eye of the beholder.
> It is more blessed to add than to multiply.
> Gaze upon this alternative:
> 
> def printitems2(sequence, count=5):
>     """Print count items of sequence per line."""
>     for pos in range(0, len(sequence), count):
>         for item in sequence[pos:pos+count]:
>             print item,
>         print


You know, that's so obvious that I've just slapped myself for not thinking
of it first. What *was* I thinking???


-- 
Steven D'Aprano 




More information about the Python-list mailing list