Iterating two arrays at once

Discussion in 'Python' started by mathieu, Aug 29, 2008.

  1. mathieu

    mathieu Guest

    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

    Thanks !
     
    mathieu, Aug 29, 2008
    #1
    1. Advertising

  2. 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
     
    Bruno Desthuilliers, Aug 29, 2008
    #2
    1. Advertising

  3. Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
    > 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


    Hey,

    zip is your friend:

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

    does what you want. If you deal with big lists, you can use izip from
    itertools, which returns a generator.

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

    HTH

    Matthias
     
    Matthias Bläsing, Aug 29, 2008
    #3
  4. mathieu

    mathieu Guest

    On Aug 29, 12:46 pm, Matthias Bläsing <matthias.blaes...@rwth-
    aachen.de> wrote:
    > Am Fri, 29 Aug 2008 03:35:51 -0700 schrieb mathieu:>
    >
    > > 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

    >
    > Hey,
    >
    > zip is your friend:
    >
    > for a,b in zip(A,B):
    > print a,b
    >
    > does what you want. If you deal with big lists, you can use izip from
    > itertools, which returns a generator.
    >
    > from itertools import izip
    > for a,b in izip(A,B):
    > print a,b


    Thanks all !
     
    mathieu, Aug 29, 2008
    #4
  5. mathieu a écrit :
    (snip solution)

    > Thanks all !


    FWIW, this has been discussed here *very* recently (a couple hours ago).
    Look for a thread named "iterating over two arrays in parallel?", and
    pay special attention to Terry Reedy's answer.
     
    Bruno Desthuilliers, Aug 29, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan
    Replies:
    3
    Views:
    302
    =?iso-8859-15?Q?Pierre-Fr=E9d=E9ric_Caillaud?=
    Jul 25, 2004
  2. Gal Diskin

    Iterating over several lists at once

    Gal Diskin, Dec 13, 2006, in forum: Python
    Replies:
    17
    Views:
    464
    Roy Smith
    Dec 27, 2006
  3. Replies:
    4
    Views:
    322
    Cousin Stanley
    Aug 29, 2008
  4. carl
    Replies:
    5
    Views:
    2,459
    James Kanze
    Nov 25, 2009
  5. Kev Jackson
    Replies:
    2
    Views:
    136
Loading...

Share This Page