[Tutor] Print items from 3 lists in order

Alan Gauld alan.gauld at btinternet.com
Mon Oct 22 02:20:55 CEST 2012


On 22/10/12 01:09, Dave Angel wrote:

>> I tried:
>>>>> for x in zip(a, b, c): print x
>> But that gives:
>> ('Ron', '25th oct', 'Charms')
>> ('Harry', '27th oct', 'DADA')
>> ('Hermoine', '29th oct', 'Potions')
>>
>> ???

So all you have now is a formatting problem.

> Very good;   you're close.  instead of z, just use 3 variables:
>
> for xname, xdate, xitem in zip(a,b,c):
>       print xname, "-", xdate
>       print xitem
>
> or something similar.

Which could be a format string (Python 2 format since that
looks like what you are using):

for x in zip(a, b, c):
     print "%s - %s\n%s" % x

The advantage of the format string is it gives you much more
control over spacing and justification of the items.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list