A for loop is the workhorse of Python, and the difference between a loop that reads cleanly and one that fights you comes down to three decisions. You have to decide what you are looping over, whether you need the position of each item, and what you want to happen when the loop ends early. This lesson covers the mechanics of range(), the two helper functions that make loops pleasant to write, and the off-by-one habits that are worth unlearning.
The previous lesson in this module covered "Comparison and Logical Operators in Real Conditions", and the next lesson in this module is about "while Loops and Breaking Out Safely".
How does a for loop work?
Underneath, a for loop asks the object it was given for an iterator, and then it pulls one value at a time until the iterator is empty. Any object that supports that pattern can be looped over, which includes lists, tuples, strings, dictionaries, sets, files, and anything that you write yourself.
languages = ['Python', 'Rust', 'Go']
for language in languages:
print('I write ' + language)
print('the loop variable survives the loop ->', language)
for character in 'Tutorsbot':
print(character, end=' ')
print()
Two side effects of that mechanism are worth knowing about. The loop variable is still bound to its last value once the loop has finished, which is occasionally useful and occasionally a trap when the loop never ran at all. And the loop variable does not have to be unique, because assigning to it inside the body simply overwrites it. That is why using a name like i inside a nested loop will overwrite the outer one.
One rule saves a lot of debugging time. Do not modify the sequence that you are looping over. Python walks a list by keeping track of an index, so removing an item shifts the remaining items to the left and the loop can skip one of them. Appending to the list can keep the loop running far longer than you expect. If you need to filter, build a new list with a comprehension, or collect the items that you want into a separate list and act on it afterwards.
How do I use range() correctly?
range() produces a sequence of numbers. It takes a start, a stop, and a step. The start is included, and the stop is not. That single rule explains every surprise that people have with it. range(1, 5) is 1, 2, 3, and 4, and it is never 5, because the stop value marks where the range ends and not a value that it contains.
print('range(5) ->', list(range(5)))
print('range(2, 6) ->', list(range(2, 6)))
print('range(0, 10, 2) ->', list(range(0, 10, 2)))
print('range(5, 0, -1) ->', list(range(5, 0, -1)))
print('len of a big range ->', len(range(1000000)))
squares = range(1, 6)
print('type of range ->', type(squares).__name__)
print('sum of squares ->', sum(number * number for number in squares))
Since Python 3, range() does not build a list. It is a small object that remembers its start, its stop, and its step, and it produces values as they are needed. A range that covers a million numbers therefore costs almost nothing in memory. That is why range(1000000) is safe inside a loop, while list(range(1000000)) allocates eight megabytes of memory.
A negative step counts backwards, and the same rule about the excluded stop still applies. That is why range(5, 0, -1) ends at 1 and does not include 0. If you want the last item as well, write range(len(items) - 1, -1, -1).
Here is a note on choosing between for and while. Use for whenever you are walking through a known collection of things, because the loop variable, the ending condition, and the step are all handled for you. Use while only when the number of rounds genuinely depends on something that is worked out as you go, such as retrying an operation until it succeeds. That case gets its own lesson next.
One more habit is worth forming early. Let the loop read as a sentence about the data and not about the mechanics. for customer in customers says what you are doing, and for i in range(len(customers)) says how you are counting. The first version survives a change in the data structure underneath, and it keeps working if somebody later hands you a generator instead of a list.
Why should I use enumerate() instead of counting by hand?
Manual counters work right up until the loop grows a continue, and then they are off by one. enumerate() hands you the position and the value together, so there is no counter to maintain and no way for it to drift out of step.
items = ['keyboard', 'monitor', 'cable']
for index, item in enumerate(items):
print(index, ':', item)
print()
for number, item in enumerate(items, start=1):
print(number, '.', item)
The start argument is what makes enumerate() useful for output that people read, where numbering from one is the normal convention. The same function also works with any iterable, including a dictionary, where it numbers the keys for you.
The deeper habit here is to let the language give you what you need. Python has a function for getting the index, a function for pairing two sequences, and a function for reversing an iterable. Reaching for them is faster than writing them again yourself, and it is much harder to get wrong.
What does zip() do, and what is strict=True for?
zip() walks several iterables in parallel and gives you tuples of the matching items. It stops when the shortest one runs out, and that quietly throws away any extra data.
names = ['Ada', 'Grace', 'Alan']
scores = [98, 95, 91]
for name, score in zip(names, scores):
print(name, 'scored', score)
print()
print('zip stops at the shortest ->', list(zip(names, scores, [1, 2])))
try:
list(zip(names, [1, 2], strict=True))
except ValueError as error:
print('strict=True complained ->', error)
strict=True, which arrived in Python 3.10, turns a silent truncation into an error. When the iterables have different lengths, zip raises ValueError instead of quietly dropping the extras. That one keyword turns a data problem that would otherwise show up later as a mysterious empty field into an immediate and obvious failure.
When you are working with dictionaries, items() gives you the same pairing in a tidier form, and sorted() accepts a key function when you want to order by something other than the default.
stock = {'apples': 12, 'pears': 7, 'plums': 30}
for fruit, count in stock.items():
print(fruit, count)
print()
print('sorted by name ->', sorted(stock))
def count_of(item):
"""Return the count from a (name, count) pair."""
return item[1]
print('sorted by count ->', sorted(stock.items(), key=count_of, reverse=True))
print('reversed ->', list(reversed(['a', 'b', 'c'])))
Your turn: build an index table
- Write a script with a list of task names and a matching list of owners, and print a numbered table using
enumerate(names, start=1)andzip()together, so that each line shows the number, the task, and the owner. - Add a check with
zip(..., strict=True)and then deliberately shorten one of the lists. Read theValueErrorand explain in a comment why you would rather see that error than silently lose a row. - Replace the manual counter in your first draft with
enumerate(), and confirm that the output is identical. - Loop over a dictionary of stock levels with
items(), and print only the items that are below a threshold, using anifinside the loop rather than filtering the dictionary first. - Finish with a
forloop overrange(1, 6)that prints the first five multiples of a number that you choose. Check that it stops before the sixth multiple, and explain the excluded stop in a comment.
Loop recipes
- A
forloop pulls one value at a time from any iterable, and the loop variable keeps its last value afterwards. range()has an included start and an excluded stop, it supports a negative step, and it does not build a list in memory.- Use
enumerate()instead of a counter that you write by hand, and passstart=1when people will read the numbers. zip()pairs sequences and stops at the shortest one. Passstrict=Trueto turn a truncation into an error.- Use
items()for dictionaries,sorted(key=...)for custom ordering, andreversed()to walk backwards.