capypad
0 day streak
python / beginner
Snippet

Parallel Looping with zip()

The zip() function allows you to combine two or more lists so you can iterate through them simultaneously. It pairs the first elements together, then the second elements, and so on.

snippet.py
python
1
2
3
4
names = ["Alice", "Bob"]
scores = [85, 92]
for name, score in zip(names, scores):
print(name, score)
Breakdown
1
names = [...], scores = [...]
Two separate lists of equal length that we want to process together.
2
for name, score in zip(names, scores):
Uses zip to pair each name with its corresponding score in one loop step.
3
print(name, score)
Prints the paired data from both lists side by side.