Printing lists in columns (was: TypeError: 'module object is not callable')

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Tue Sep 4 06:50:37 EDT 2007


On 2007-09-04, Chris.Tidy at jet.uk <Chris.Tidy at jet.uk> wrote:
> Thanks guys. Changing to how Python does things has a lot of geting
> used to!

That's part of the fun :-)

> Do any of you have any ideas on the best way to do the following
> problem:
>
> 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



Good luck,
Albert



More information about the Python-list mailing list