Printing lists in columns

Amit Khemka khemkaamit at
Wed Sep 5 10:31:49 CEST 2007

On 9/5/07, cjt22 at <cjt22 at> wrote:
> Thanks guys, I really appreciate it. I have never used google groups
> before and am so impressed with how helpful you all are. It is also lovely
> that none of you mock my little knowledge of Python but just want to
> improve it.

And we are proud of it !

> I have another question in relation to the izip_longest function (I
> persume
> this should be within the same topic).
> Using this funciton, is there a way to manipulate it so that the
> columns can be formated
> tabular i.e. perhaps using something such as str(list).rjust(15)
> because currently the columns
> overlap depending on the strings lengths within each column/list of
> lists. i.e. my output is
> currently like:
> bo, daf, da
> pres, ppar, xppc
> magnjklep, *, dsa
> *, *, nbi
> But I want it justified, i.e:
> bo       ,  daf,  da
> pres     , ppar,  xppc
> magnjklep,    *,  dsa
> *        ,    *,  nbi

You can format the output while "print"ing the table. Have a look at:

for tup in  izip_longest(*d, **dict(fillvalue='*')):
    print "%15s, %15s, %15s" %tup   # for a tuple of length 3, you can
generalize it

> I am struggling to understand how the izip_longest function works
> and thus don't really know how it could be manipulated to do the
> above.
> It would be much apprecited if somoene could also explain how
> izip_function
> works as I don't like adding code into my programs which I struggle to
> understand.
> Or perhaps I have to pad out the lists when storing the Strings?
> Any help would be much appreciated.

This is an example of "generator" functions, to understand what they
are and how they work you can:
1. web-search for "python generators"
2. have  a look at "itertools" module, for more generators

Amit Khemka

More information about the Python-list mailing list