Iterating Without enumerate()
I will use VS Code for my coding, so when I’m writing code and then a filename—in this case,
plus_one.py—VS Code either opens the
plus_one file or creates a new one if it doesn’t exist. In this case, it doesn’t exist yet, so once I hit Enter, it opens up a new file. It has the dot because it’s not saved yet, so if I’m saving it, it’s there.
So as you can see of the
ls command, now you’ve created the
plus_one file, and this is the file that we’ll start coding in.
As you saw in the intro of this course, we will rely on the
seasons is a list, and it contains four strings:
Winter. To get things started, just create a
for season in seasons:, and then
So it’s important here to note the
seasons list ends with an s, and the variable is only
seasons. So here, you want to print the
season variable. When you save it and run it
in the terminal by using
for loop in Python uses collection-based iteration.
This means that Python assigns the next item from an iterable to the loop variable on every iteration. In this case, the variable is
season, and you see the value by printing it.
That’s what you see here in the terminal. When you also want to see the count, you have to create a
count variable in this case,
and you give it the value
count is an integer that keeps track of how far into the list you are, so if you print the
count variable as well, and you run the file again, you see for now, it’s staying with
1 because you’re not increasing it, so you also want to increase the
count variable every time you’re stepping over it.
So you do this by
count += 1.
When you run it again, you see the output that you wanted to have:
3 Fall, and
4 Winter. All right, that works.
Let’s try another way. Before you move on to the next file, you can copy the content of line 3, where you store the
seasons variable, so you don’t have to type it next time.
For the next example, create a file again—here, I’m using the
code command because I’m using VS Code—and pass in the new filename, which is
03:32 Since this file doesn’t exist yet, I created a new one with this command, and with the dot, you’re seeing here on the tab that I have to save it. And once I’m saving it, the file exists.
But of course, depending on the editor you are using, you can use a different command to open and create your file. If you have copied the
seasons variable from the
plus_one file, you can just paste it here or write it again in line 3, so you have
And like I said before, this time you will explore a different way of looping through the
and the main difference here is that you are relying on the
count variable and using
range() to do so.
range() takes two parameters.
One is the starting point of a range, and second is the
stop parameter. If you don’t pass in the start, it starts with
0. This is fine by us. You will see in a second why.
stop parameter is the
len() (length) of the
seasons variable. So the length of
seasons is four, so you are looping four times and getting the
count variable value back in each step. To see what this is, you can print
So if you’re running the file
python3 range_len.py, then you will see that
count starts with
3, and now to also get the content of the
seasons list, you can access its index because the count is actually the index of
seasons that you want to access.
So if we are using
seasons[count], you will get the index in each step, which starts with
0 and then
Let’s run it again to see what it is. So the
0 index of
Winter. This looks almost as you want it to be, but you have a problem here. You’re starting with
0, so you have to increase the
count variable for the
print() statement so it starts with
1. When you run it again, then you have exactly the print that you wanted to.
So these are two examples how to count without
enumerate(), but both examples, the
plus_one.py example and the
range_len.py example, have shortcomings. So let’s explore them a little bit.
Now you have a look at the problems of the two scripts you just created. The first one,
plus_one.py, is a script that every one of us has written at some time, and I mean, we just did it in this lesson.
And the first thing that’s not ideal is that you declare the
count variable outside of the
for loop, and you refer to it always inside of the
for loop. So it’s outside of the scope where you actually do the work, so where you have the
Then, you must not forget to increment the
count variable, and more so, you have to increment it after the
print() statement. If you will increment it before the
print() statement, then you would start with
3 Summer, and so on.
07:21 So you have to look in which order your statements are.
The second script you created,
range_len.py, also has an issue. And if you look at it just with this slide, it might be compared to the first one or be very visible that it’s harder to read.
There is this there’s one line with all the parenthesese—
range(len(seasons))—and then you have a similar problem like you had before, with the
count variable. But this time you’re referring to the
seasons variable outside of it.
So you have also, again, a variable that’s not inside the scope of your
for loop. And the last thing which is an issue with this one is that this one would fail if
seasons is a set.
range(len(seasons)) works because
seasons is a list. But if this one would be a set, it wouldn’t work anymore because you can’t access the index of a set.
This only works with a list in this case. So let’s see how
enumerate() fixes those shortcomings.
Become a Member to join the conversation.