iterating over two arrays in parallel?
tjreedy at udel.edu
Fri Aug 29 08:00:03 CEST 2008
mh at pixar.com wrote:
> I want to interate over two arrays in parallel, something like this:
> for i,j in a,b:
> print i,j
> where i,j would be 1,4, 2,5, 3,6 etc.
> Is this possible?
How to fish for yourself:
search 'Python loop two arrays parallel' and second hit with Google is
which has this entry
"To loop over two or more sequences at the same time, the entries can be
paired with the zip() function.
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print 'What is your %s? It is %s.' % (q, a)
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
Or go to the Tutorial directly, expand the chapter headings, and notice
that 5. Data Structures has section 5.6 Looping Techniques.
Indeed, I recommend that you read thru at least the first 9 chapters.
More information about the Python-list