newbie: how to loop through multiple lists

Alex Martelli aleax at aleax.it
Wed Oct 2 06:17:11 EDT 2002


Alexander Eisenhuth wrote:

> Hallo alltogether,
> 
> is there a expression to loop through two lists in one iteration? For
> instance:
> 
> --
> 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.


Alex




More information about the Python-list mailing list