iterating over two arrays in parallel?

Cousin Stanley cousinstanley at gmail.com
Fri Aug 29 09:47:30 EDT 2008


> I want to interate over two arrays in parallel, 
> something like this:
>
>     a=[1,2,3]
>     b=[4,5,6]
>
>     for i,j in a,b:
>         print i,j
>
> where i,j would be 1,4,    2,5,   3,6  etc.
>
> Is this possible?
>
> Many TIA!
> Mark

>>>
>>> list_1 = range( 1 , 4 )
>>> list_2 = range( 4 , 7 )
>>>
>>> list12 = zip( list_1 , list_2 )
>>>
>>> for this in list12 :
...     print '   ' , this
...
    (1, 4)
    (2, 5)
    (3, 6)
>>>
>>> for i , j in list12 :
...     print '   ' , i , j
...
    1 4
    2 5
    3 6


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona




More information about the Python-list mailing list