Show the zip approach and the nested-loop approach for pairing two sequences, and compare them.
The zip function is the built-in way to walk through several sequences at the same time. It gives you one tuple per round, and you can name each part of the tuple in the loop.
names = ['ada', 'grace', 'alan']
scores = [99, 87, 91]
# zip is the short and clear way.
for name, score in zip(names, scores):
print(name, score)
# The explicit nested loop does the same job in more lines.
for index in range(len(names)):
print(names[index], scores[index])
# zip together with enumerate gives you the index as well.
for index, pair in enumerate(zip(names, scores)):
print(index, pair[0], pair[1])
All three loops print the same three pairs. The zip version is the one you should write, because the nested loop repeats the indexing on every line, it has to assume that both lists are the same length, and it becomes harder to read every time you add another sequence. The third version is worth knowing, because combining enumerate with zip is the normal way to get an index and a pair of values at the same time.
The most important behaviour to remember is that zip stops at the shortest sequence. If scores were shorter than names, the extra names would be dropped without any warning at all. That is exactly what you want when you are pairing two files of equal length, and it is a real trap when the lengths are supposed to match but do not.
Two smaller details matter as well. zip is lazy, so it does not build a list of pairs in memory unless you wrap it in list(). And it works with any number of sequences, not only two, so a three-way pairing is simply zip(a, b, c). If you want to keep every value when the lengths differ, the next answer shows you the tool for that job.