Loop in a loop?

Quentin Gallet-Gilles qgallet at gmail.com
Thu Jan 17 13:32:53 CET 2008


Hi,

I'd consider using zip :

>>> array1 = ['one','two','three','four']
>>> array2 = ['a','b','c','d']
>>> zip(array1, array2)
[('one', 'a'), ('two', 'b'), ('three', 'c'), ('four', 'd')]
>>> for one, two in zip(array1, array2):
...     print one, two
...
one a
two b
three c
four d
>>>

HTH,
Quentin

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

> Hi,
> I'm new to Python and have come across a problem I don't know how to
> solve, enter com.lang.python :)
>
> I'm writing some small apps to learn the language, and I like it a lot
> so far.
>
> My problem I've stumbled upon is that I don't know how to do what I
> want. I want to do a loop in a loop. I think.
>
> I've got two arrays with some random stuff in, like this.
>
> array1 = ['one','two','three','four']
> array2 = ['a','b','c','d']
>
> I want to loop through array1 and add elements from array2 at the end,
> so it looks like this:
>
> one a
> two b
> three c
> four c
>
> I'm stuck. I know how to loop through the arrays separatly and print
> them, but both at the same time? Hmmm.
>
> A push in the right direction, anyone?
>
> R,
> SH
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080117/856d9b9a/attachment.html>


More information about the Python-list mailing list