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.
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
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.
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
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.
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!—
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.
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.
3 Winter should actually be
2 Summer, and so on.
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
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
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.
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
season are accessed in the
print() statement and you just create them with the
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
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.