capypad
0 day streak
python / intermediate
Snippet

Parallel Iteration with zip()

The zip() function takes multiple iterables and aggregates them into tuples. This is the most efficient way to iterate over multiple lists in parallel.

snippet.py
python
1
2
3
4
5
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
 
for name, score in zip(names, scores):
print(f"{name} scored {score}")
Breakdown
1
zip(names, scores)
Creates an iterator that yields pairs of elements from both lists.
2
for name, score in ...
Unpacks each tuple into individual variables during iteration.