Using enumerate()
00:00
In the last lesson, you created two scripts where, in the end, you learned why they are not ideal to solve the issue of counting the seasons, And now it’s time to see how the enumerate()
function solves those problems and makes writing code a little bit more clean.
00:19
So let’s get started. Here again, we are in VS Code and I create a new file with the code
command. Depending on the editor that you are in, you have to create a file a little bit different, but also call the file enumerate.py
.
00:40
So we’re both knowing what we’re talking about. So by using the code
command with VS Code, I either open the enumerate
file or I create it if it doesn’t exist. In this case, it didn’t exist.
00:54
You see on the dot here that it’s not saved yet. So let’s save it and start with this creatively named file to explore the enumerate()
01:08
function. All right, again, I paste the seasons
list. If you haven’t saved it in your clipboard, just go back to the files that you create in the last lesson, or type the seasons
variable again.
01:24
And as you will see in a second, you will do basically two steps in one with the enumerate()
function. So in the last lesson you either used the season
variable, or you used the count
variable when you looped through the seasons or the range of the length of the seasons. This time, you can do both at once, for count
and season in
—and here it comes!—enumerate()
.
01:57
And there you pass in the seasons
variable, then you can print the count and the season immediately without accessing some variables outside of the loop or using the range
. So I saved it.
02:19
And we’re almost there again. We have the 0
index at the beginning, but apart from this, this looks exactly like the output that you’re aiming for.
02:30
So 0 Spring
, 1 Summer
, 2 Fall
, 3 Winter
should actually be 1 Spring
, 2 Summer
, and so on.
02:38
So instead of counting the count
variable one up here, the enumerate()
function actually accepts another argument, and this is the start
parameter, and you can give the start
parameter a 1
.
02:54
And if you run the code again, then you have exactly the output that you wanted to have. So there is a bit happening here. This was a quite fast run-through, so let’s investigate a little bit what’s happening here with the enumerate()
function.
03:15
But before going deeper and looking what exactly the enumerate()
function is doing, let’s have a more high-level look at what you just created.
03:25
Maybe you already experienced while writing the code that it’s much more clean and concise compared to the other both scripts that you created. The other advantage is you don’t have to access any variables outside of the for
loop.
03:40
So count
and season
are accessed in the print()
statement and you just create them with the enumerate()
function.
03:47
You’re not accessing some variable, like in the plus_one
example, that is outside of it that you’re counting up or like in the range_len
example that you have to access the index of a list inside of the for loop
.
04:04
So in summary, the advantage of the enumerate()
function is that it’s clean and concise and that using it, you’re creating a for
loop that is self-contained, but there are a few things happening under the hood. In the next lesson, you will find out what’s happening there.
Become a Member to join the conversation.