Example 1 — Pair Names with Ages
Loop over two lists together and turn the pairs into a list and a dict.
names = ["Ana", "Ben", "Cy"]
ages = [31, 25, 40]
for name, age in zip(names, ages):
print(name, age)
print(list(zip(names, ages)))
print(dict(zip(names, ages))) How It Works
- Parallel loop. Each step takes one name and the age at the same position.
- Tuples.
list(zip(...))shows the pairs zip produces. - Instant dict.
dict()accepts the pairs and uses names as keys. - Fresh zip each time. Each line calls
zip()again, because an iterator can be used only once.

