python / beginner
Snippet
Pairing Data with zip()
The zip() function takes two or more iterables and aggregates them into a single iterable of tuples. It pairs elements based on their position, making it easy to process related lists simultaneously.
snippet.py
1
2
3
4
names = ['Alice', 'Bob']scores = [85, 92]for name, score in zip(names, scores):print(name, score)
Breakdown
1
names = ['Alice', 'Bob']
Create a list of strings representing names.
2
scores = [85, 92]
Create a list of integers representing numerical scores.
3
for name, score in zip(names, scores):
Loop through both lists together, pairing the first name with the first score, and so on.
4
print(name, score)
Print the paired values from both lists in each iteration.