iterating over two arrays in parallel?

skip at pobox.com skip at pobox.com
Thu Aug 28 21:39:34 EDT 2008


>>>>> "mark" == mh  <mh at pixar.com> writes:

    mark> I want to interate over two arrays in parallel, something like this:
    mark>     a=[1,2,3]
    mark>     b=[4,5,6]

    mark>     for i,j in a,b:
    mark>         print i,j

    mark> where i,j would be 1,4,    2,5,   3,6  etc.

    a = [1,2,3]
    b = [4,5,6]
    for (i,j) in zip(a,b):
      print i, j

To avoid recreating the entire list you can substitute itertools.izip for
zip.

Skip




More information about the Python-list mailing list