+3 votes
in Programming Languages by (54.5k points)
What is the Pythonic way to loop over multiple lists at the same time?

1 Answer

+3 votes
by (37.5k points)
selected by
Best answer

The entries of sequences can be paired with the zip() function to iterate at the same time.

Here is an example:

>>> aa=[1,2,3,4]

>>> bb=[11,12,13,14]

>>> for v1,v2 in zip(aa,bb):

...     print("aa value: {0}, bb value: {1}".format(v1,v2))


aa value: 1, bb value: 11

aa value: 2, bb value: 12

aa value: 3, bb value: 13

aa value: 4, bb value: 14