Printing lists in columns

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Sep 3 10:27:58 EDT 2007


cjt22 at bath.ac.uk a écrit :
> On Sep 4, 3:20 pm, Bruno Desthuilliers <bruno.
> 42.desthuilli... at wtf.websiteburo.oops.com> wrote:
> 
>>cj... at bath.ac.uk a écrit :
>>(snip)
>>
>>
>>>Thanks guys
>>
>>>I have a list of lists such as
>>> a = ["1" , "2"]  b = ["4", "5", "6"]  c = ["7",8", "9"]
>>>Stored in another list: d = [a,b,c]
>>
>>>I know this makes me sound very stupid but how would I specify
>>>in the parameter the inner lists without having to write them all out
>>>such as:
>>
>>>for row in izip_longest(d[0], d[1], d[2], fillvalue='*'):
>>>    print ', '.join(row)
>>
>>>i.e. How could I do the following if I didn't know how many list of
>>>lists I had.
>>
>>for row in izip_longest(*d, fillvalue='*'):
>>     print ', '.join(row)
>>
>>HTH
> 
> 
> I thought that but when I tried it I recieved a
> "Syntax Error: Invalid Syntax"
> with a ^ pointing to fillvalue :S
> 
Yes, sorry - answered too fast, which is a cardinal sin. The 
func(*sequence) is of course the right answer, but indeed you cannot 
directly pass a keyword arg after it - you must either pass the keyword 
args first:

   izip_longest(fillvalue='*', *d)

or wrap it in a dict and use the ** notation:

   izip_longest(*d, **dict(fillvalue='*'))

In this case, the second solution only makes sens if you already have 
this dict for other reasons.



More information about the Python-list mailing list