Iterating over multiple lists (a newbie question)

Fredrik Lundh fredrik at effbot.org
Wed Jan 3 17:03:53 EST 2001


Victor Muslin wrote:
> I would like something like this (which obviously does not work):
>
> for one,two in list1, list2:
>    print one,two

in 2.0, use "zip":

    for one, two in zip(list1, list2):
        ...

in earlier versions, you can use range or map(None):

    for i in range(len(list1)):
        one, two = list1[i], list2[i]

    for one, two in map(None, list1, list2):
        ...

hope this helps!

cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list