Loop in a loop?

Quentin Gallet-Gilles qgallet at gmail.com
Thu Jan 17 09:05:56 EST 2008


On Jan 17, 2008 2:38 PM, Sacred Heart <scrdhrt at gmail.com> wrote:

> On Jan 17, 1:35 pm, cokofree... at gmail.com wrote:
> > for i in zip(array1, array2):
> >     print i
> >
> > Although I take it you meant four d, the issue with this method is
> > that once you hit the end of one array the rest of the other one is
> > ignored.
>
> Yes, small typo there.
>
> Okey, so if my array1 is has 4 elements, and array2 has 6, it won't
> loop trough the last 2 in array2? How do I make it do that?
>
>
In that case, you can use map(). It pads the shortest list with None.

>>> array1 = ['one','two','three','four', 'five', 'six']
>>> array2 = ['a','b','c','d']
>>> map(None, array1, array2)
[('one', 'a'), ('two', 'b'), ('three', 'c'), ('four', 'd'), ('five', None),
('six', None)]

Quentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080117/fac8796b/attachment-0001.html>


More information about the Python-list mailing list