Iterating over multiple lists (a newbie question)

Prateep Siamwalla teep at inet.co.th
Wed Jan 3 21:17:05 EST 2001


This is what I usually do when I run into your situation

a = [1,2,3,4,5]
b = ['a','b','c','d','e']

c = zip(a,b)
for i in c:
    print i[0],i[1]


Victor Muslin <victor at prodigy.net> wrote in message
news:3a539f3a.445366109 at localhost...
> This may be rather silly, but I can't think of a clever way...
>
> To traverse a single list the code is:
>
> for item in list:
> print item
>
> However, if there are multiple lists (of the same length) is there a
> cleverer way to do the following:
>
> for i in range(0,len(list1)):
> print list1[i], list2[i]
>
> I would like something like this (which obviously does not work):
>
> for one,two in list1, list2:
> print one,two
>
> TIA





More information about the Python-list mailing list