Iterating Without enumerate()
00:00
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.
00:29
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.
00:45
As you saw in the intro of this course, we will rely on the seasons
variable. seasons
is a list, and it contains four strings: Spring
, Summer
, Fall
,
01:03
and Winter
. To get things started, just create a for
loop, for season in seasons:
, and then print()
the season
variable.
01:17
So it’s important here to note the seasons
list ends with an s, and the variable is only season
, not seasons
. So here, you want to print the season
variable. When you save it and run it
01:36
in the terminal by using python3 plus_one.py
,
01:42
you print Spring
, Summer
, Fall
, and Winter
, A for
loop in Python uses collection-based iteration.
01:51
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.
02:03
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,
02:15
and you give it the value 1
. 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.
02:43
So you do this by count += 1
.
02:52
When you run it again, you see the output that you wanted to have: 1 Spring
, 2 Summer
, 3 Fall
, and 4 Winter
. All right, that works.
03:03
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.
03:18
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 range_len.py
.
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.
03:49
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 seasons
ready.
04:10
And like I said before, this time you will explore a different way of looping through the seasons
variable,
04:19
and the main difference here is that you are relying on the count
variable and using range()
to do so. range()
takes two parameters.
04:31
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.
04:43
And the 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 count
.
05:05
So if you’re running the file python3 range_len.py
, then you will see that count
starts with 0
, 1
, 2
, 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.
05:35
So if we are using seasons[count]
, you will get the index in each step, which starts with 0
and then 1
, 2
, and 3
.
05:49
Let’s run it again to see what it is. So the 0
index of seasons
is Spring
, Summer
, Fall
, and 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.
06:18
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.
06:35
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.
06:49
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 print()
statement.
07:06
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 2 Spring
, 3 Summer
, and so on.
07:21 So you have to look in which order your statements are.
07:27
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.
07:43
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.
07:58
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.
08:11
So 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.
08:24
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.