Introduction
Two built-in functions make loops over lists cleaner: enumerate adds a counter, and zip pairs up items from several lists.
Index arithmetic such as names[i] and marks[i] is a common source of small mistakes. These two functions let the loop hand you exactly what you need, which is both shorter and safer.
How it works
A counter with enumerate
names = ["Rahim", "Nadia", "Karim"]
for position, name in enumerate(names, start=1):
print(position, name)enumerate(names, start=1) gives pairs: (1, 'Rahim'), (2, 'Nadia'), (3, 'Karim'). The loop unpacks each pair into position and name. Without start=1 the count begins at 0.
The older way
names = ["Rahim", "Nadia"]
for i in range(len(names)):
print(i, names[i])for i in range(len(names)) with names[i] works, but it is longer and easier to get wrong. Prefer enumerate when you need the position, and plain for name in names when you do not.
Syntax
for i, name in enumerate(names, start=1):
print(i, name)
for name, mark in zip(names, marks):
print(name, mark)
Example
names = ["Rahim", "Nadia", "Karim"]
marks = [72, 91, 85]
for name, mark in zip(names, marks):
print(name, mark)
best = max(zip(marks, names))
print("Top:", best[1])zip(names, marks) pairs the first name with the first mark, the second with the second, and so on. max(zip(marks, names)) compares the pairs by mark first, so it finds the top student: Nadia, with 91.
Line by line
namesandmarksare two lists in matching order.zip(names, marks)makes pairs, and theforline unpacks each one.- Each pass prints one name and its mark.
max(zip(marks, names))compares the pairs by mark first and finds the largest.best[1]is the name part of that pair.
Expected result
Rahim 72
Nadia 91
Karim 85
Top: Nadia
In the real world
Data often arrives as matching columns, such as dates and prices, and zip joins them back up. Numbered lists and menus are the everyday use of enumerate.
Common mistakes
- Lists of different lengths:
zipstops at the shortest one and silently drops the rest. - Forgetting to unpack:
for pair in enumerate(names)gives tuples like (0, 'Rahim'). - Expecting
zip(...)to print as a list: it is lazy; uselist(zip(...))to see it.
Best practices
- Use
for x in itemswhen you do not need the position. - Use
enumeratewhen you need the position, andzipfor parallel lists. - Check that lists have the same length before relying on
zip.
Summary
enumerate adds a counter to a loop, and zip pairs up several lists and stops at the shortest. Unpacking in the for line gives each part a name.
Practice
Given names and marks, print a numbered list like 1. Rahim: 72 using enumerate and zip together.
Quick check
Key takeaways
- enumerate gives you the position without any index arithmetic.
- zip stops at the shortest list.
- Unpacking keeps a loop short and clear.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.