Iterating two arrays at once

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Aug 29 06:44:34 EDT 2008


mathieu a écrit :
> Hi there,
> 
>   just trying to figure out how to iterate over two array without
> computing the len of the array:
> 
>   A = [1,2,3]
>   B = [4,5,6]
>   for a,b in A,B: # does not work !
>     print a,b
> 
> It should print:
> 
>   1,4
>   2,5
>   3,6

for a, b in zip(A, B):
     print a, b

or, using itertools (which might be a good idea if your lists are a bit 
huge):

from itertools import izip
for a, b in izip(A, B):
     print a, b





More information about the Python-list mailing list