Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - write list list of lists in columns

Tags:

python

list

csv

I have a list of lists with a various number of elements (int). I want to print/write it, but in columns rather than in rows.

Example:

l = [[1,2,3],[4,5],[6,7,8,9],[0]]

Result:

1 4 6 0
2 5 7 .
3 . 8 .
. . 9 .
like image 762
WlJs Avatar asked Feb 05 '11 19:02

WlJs


1 Answers

The easiest way to do this is to use itertools.izip_longest():

for x in itertools.izip_longest(*l, fillvalue="."):
    print " ".join(str(i) for i in x)
like image 133
Sven Marnach Avatar answered Sep 17 '22 22:09

Sven Marnach