Iterating over multiple lists (a newbie question)

Prateep Siamwalla teep at inet.co.th
Thu Jan 4 02:00:32 EST 2001


woops, i should have run the loop this way:
> a = [1,2,3,4,5]
> b = ['a','b','c','d','e']
>
> c = zip(a,b)
> for i,j in c:
>     print i,j



Prateep Siamwalla <teep at inet.co.th> wrote in message
news:930mb2$u7p$1 at news.inet.co.th...
> 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