[Tutor] Iterating over two sequences in "parallel"

Wayne Werner waynejwerner at gmail.com
Sat Nov 28 14:09:14 CET 2009


On Sat, Nov 28, 2009 at 6:59 AM, Dave Angel <davea at ieee.org> wrote:

>
> And if the lists are large, use  itertools.izip() which works the same, but
> produces an iterator.
>
> Note that if the lists are not the same length, I think it stops when the
> shorter one ends.
>

But you can use izip_longest:

import itertools
list1 = [1,2,3,4]
list2 = [1,2]

lIn [32]: for x, y in itertools.izip_longest(list1, list2):
   ....:     print x,y
   ....:
   ....:
1 1
2 2
3 None
4 None

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091128/0030d4c8/attachment.htm>


More information about the Tutor mailing list