Python - list of lists of lists in columns

I have a list of lists with a different number of elements (int). I want to print / write it, but in columns, not 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 . 
+6
python list csv
source share
2 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) 
+14
source share

It:

 import itertools l = [[1,2,3],[4,5],[6,7,8,9],[0]] for t in itertools.izip_longest(*l): print "".join("%3d" % x if x is not None else " ." for x in t) 

gives:

  1 4 6 0 2 5 7 . 3 . 8 . . . 9 . 
+3
source share

All Articles