Printing lists in columns (was: TypeError: 'module object is not callable')
Duncan Booth
duncan.booth at invalid.invalid
Tue Sep 4 07:33:00 EDT 2007
"A.T.Hofkamp" <hat at se-162.se.wtb.tue.nl> wrote:
>> Each loop I perform, I get a new list of Strings.
>> I then want to print these lists as columns adjacent to each other
>> starting with the first
>> created list in the first column and last created list in the final
>> column.
>
> Use zip:
>
>>>> x = ['1', '2']
>>>> y = ['3', '4']
>>>> for row in zip(x,y):
> ... print ', '.join(row)
> ...
> 1, 3
> 2, 4
>
>
> zip() constructs a list of rows, like
>
> [('1', '3'), ('2', '4')]
>
> which is then quite easy to print
But watch out if the lists aren't all the same length: zip won't pad out
any sequences, so it may not be exactly what is wanted here:
>>> x = ['1', '2', '3']
>>> y = ['4', '5']
>>> for row in zip(x,y):
print ', '.join(row)
1, 4
2, 5
>>>
More information about the Python-list
mailing list