Syntax for simultaneous "for" loops?

Padraig at Linux.ie Padraig at Linux.ie
Mon Feb 17 06:26:38 EST 2003


John Ladasky wrote:
> Hi there,
> 
> A few weeks ago I was agonizing over choosing a new programming
> language, after not having programmed for years.
> 
> http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=c09b237b.0302060136.5683054e%40posting.google.com
> 
> I appear to have chosen Python.  I am really pleased with how quickly
> I am getting results.
> 
> A few days ago I came across a web page which described a "for"
> syntax, which allowed two variables to be stepped at the same time,
> through two independent lists.  I said to myself, "that's cool, I
> wonder when I might need that?"  Well, I need it now.  But I
> bookmarked the relevant web page at work, and we're snowed in here!
> 
> Would some kind soul please point me to the relevant information? 
> Thanks!

You probably want to use zip() in conjunction with tuple expansion:

l1=["a",'b','c']
l2=[1,2,3]
for one, two in zip(l1,l2):
     print one, two

Pádraig.





More information about the Python-list mailing list