python / beginner
Snippet
Parallel Iteration with zip()
The zip() function takes multiple iterables and aggregates them into a single iterator of tuples. It allows you to loop through two or more lists at the same time.
snippet.py
1
2
3
4
5
6
names = ['Alice', 'Bob']scores = [85, 92]for name, score in zip(names, scores):print(name)print(score)
Breakdown
1
for name, score in zip(names, scores):
Pairs the first name with the first score, the second with the second, etc.
2
print(name)
Prints the name from the current pair.