Iterating through two lists

Andrae Muys amuys at shortech.com.au
Mon Jun 3 21:00:02 EDT 2002


"Denis S. Otkidach" <ods at fep.ru> wrote in message news:<mailman.1022234790.26965.python-list at python.org>...
> On Fri, 24 May 2002, jb wrote:
> 
> j> I have two lists, x and y with the property len(x) = len(y).
> j>
> j> I should like to achive this (x is a list of class instances)
> j>
> j>   for (a,b) in (x,y): a.f(b)
> j>
> j> Is there a fancy way of doing this or have I to introduce an
> j> auxillary
> j> counter (that is very easy but maybe not very "lispy", that
> j> is
> j> "python-like").
> 
> If lists are not very lange try this:
> 
> for a, b in zip(x, y): a.f(b)

If the lists are large then you probably need something along the lines of xzip.

def xzip(*args):
    iters = [iter(a) for a in args]
    while 1:
        yield tuple([i.next() for i in iters])

Andrae Muys



More information about the Python-list mailing list