newbie: how to loop through multiple lists

Robert k.robert at gmx.de
Wed Oct 2 19:37:23 CEST 2002


> > l1 = ['a', 'b', 'c', 'd']
> > l2 = [ 1,  2,    3,   4]
> >
> > for lChar, lNumb in (l1, l2):
> > print lCnt, ':', lChar
> > --
> > I hope you understand what I need .
>
> Yep:
>
> for iChar, iNumb in zip(i1, i2):
>     print iChar, ':', iNumb
>
> will do it.  The zip function accepts N sequences and returns a
> list of tuples of N items each, just as you need to use here for
> the specific case N==2.
>

oops ... why did they introduce such minor elegance into the builtins and
occupy name "zip"?  I always found 'map' natural therefore - thinking of
'transforming any sequence stuff':

for iChar, iNumb in map(None, i1, i2):
   print iChar, ':', iNumb










More information about the Python-list mailing list