https://nedbatchelder.com/text/iter.html


This is a presentation I gave at PyCon 2013. You can read the slides and text on this page, or open the actual presentation in your browser (use right and left arrows to advance the slides), or watch the video:
A talk for PyCon 2013.
This talk is billed as Beginner, and sounds like a beginner topic, but I prefer to think of it as Fundamental. Plenty of expert Python programmers aren’t making enough use of the tools I’m going to talk about.
Python has a nice model of abstract iteration which can be used to increase the expressiveness of your programs. Python’s iteration tools are one of the most underused features of the language, especially by programmers coming from other “similar” languages.
My goal here is to show Python iteration in a light that would encourage programmers to explore more of its possibilities.
All programs iterate over data, of various sorts. When using iteration, you should be as direct as you can, and you should use more abstractions to make your loops as clear and direct as you can.
Let’s say you have a list of numbers, and you want to print each of them. One way to do it is shown in the first code sample: start a counter at zero, and as long as the counter is less than the length of the list, access that element of the list, and print it. Then increment the counter, and continue on to the next element. Eventually, the while loop will run over the whole list.
This while loop works, and is a least common denominator: this way of thinking about the loop can be written in almost any language.
In Python, there’s a simpler way to have a loop that ranges from zero to N-1. You can use the idiom “for i in range(N)”, as we see next. Here i again takes on values starting with zero and going up to the last index of my_list. This is the Python version of C or Javascript’s classic for-loop structure.
C programmers coming to Python often end here: they are used to a loop that iterates over integers, and range() gives them a nice compact way to do it.
But Python gives us a much more natural way to loop over the values in my_list. Why are we talking about integers? This is like a Rube Goldberg machine. We set up a mechanism to give us integers, when we don’t want the integers at all, so the first thing we do with each integer is turn it into a list element. We might has well have written, “the boot kicks the ball into the net, which tips the watering can...”
Rather than iterating over indexes, and using the index i to get the value we really want from the list, we can simply loop over the values directly.
The last code sample shows the right way to write this loop. “for v in my_list” gives us each value in my_list in the variable v, with no need to fiddle around with the index i at all. What started as a five-line while loop with two variables is now a two-line loop with only one.
The for loop is Python’s versatile swiss-army-knife iteration tool. It can iterate over all sorts of Python objects. Any Python object can be “iterable,” which means it can provide a stream of values. Any iterable can be used in a for loop. The for loop extracts values from the stream, assigns them to the name, and execute the statements in the body once for each value.
On the face of it, this seems simple. But this simplicity provides great power. The for loop can be used in all sorts of situations to iterate all sorts of values. And you can often re-shape your iterations to use the for loop more powerfully.