[Tutor] Print items from 3 lists in order

Dave Angel d at davea.name
Mon Oct 22 02:09:18 CEST 2012


On 10/21/2012 08:01 PM, Saad Javed wrote:
> Hi,
> a = ['Ron', 'Harry', 'Hermoine']
> b = ['25th oct', '27th oct', '29th oct']
> c = ['Charms', 'DADA', 'Potions']
> I want to print like this:
> Ron - 25th oct
> Charms
> Harry - 27th oct
> DADA
> Hermoine - 29th oct
> Potions
>
> The items in each list are populated dynamically so I don't know how many
> items will be there in each list, every time the program runs.
> 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')
>
> ???
>
> Saad
>
>

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.

Alternatively, you could have printed z[0], z[1], and z[2].  But this
way is clearer.



-- 

DaveA



More information about the Tutor mailing list