Iterate through two lists in parallel in Python

Iterating through a two lists in parallel is natively handled in Python using the zip function.

Iterate through two lists using zip

Python’s zip function will aggregate elements from two or more iterables.

1
2
3
4
5
foo = ["x", "y", "z"]
bar = [1, 2, 3]

for f, b in zip(foo, bar):
  print(f, b)

Output:

1
2
3
x 1
y 2
z 3

This works with any number of iterables:

1
2
3
4
5
6
foo = ["x", "y", "z"]
bar = [1, 2, 3]
baz = ["!", "@", "#"]

for f, b, z in zip(foo, bar, baz):
  print(f, b, z)

Output:

1
2
3
x 1 !
y 2 @
z 3 #

If the index is necessary, use enumerate:

1
2
3
4
5
foo = ["x", "y", "z"]
bar = [1, 2, 3]

for i, (f, b) in enumerate(zip(foo, bar)):
  print(i, f, b)

Output:

1
2
3
0 x 1
1 y 2
2 z 3