Transcript for:
Python `enumerate` for Looping with Counters

welcome to python enumerate simplify looping with counters i'm philip from real python and today i want to show you how the enumerate function helps you to count in for loops most calendar-based methods use a four season model to identify the warmest and the coldest seasons summer and winter and in between spring and fall why am i talking about seasons well in this course you will work with list of seasons spring summer fall and winter and your task is to create a python script to count them in the end the output should look like this one spring to summer three fall for winter first you will do this without the enumerate function then you will use it in lesson number three you will learn what this enumerate function actually is and then you will use this knowledge to rebuild it finally you will see your very own enumerate function in action all right let's get started i will use vs code for my coding so when i'm writing code and then a file name in this case plus underscore one dot pi 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 test the dot because it's not saved yet so if i'm saving it it's there so as you can see with the ls command now you've created the plus underscore one file and this is the file that we're start coding in 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 winter to get things started just create a for loop for season in seasons and then print the season variable it's important here to note the seasons list ends with an s and the variable is only season not season so here you want to print the season variable when you save it and run it in the terminal by using python 3 plus 1. pi you print spring summer fall and winter a 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 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 one because you're not increasing it so you also want to increase the count variable every time you are stepping over it so you do this by count plus equals one when you run it again you see the output that you wanted to have one spring two summer three fall and four 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 file name which is range underscore lan.pi since this file doesn't exist yet i created a new with this command and on 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 three so you have seasons ready and like i said before this time you will explore a different way of looping through the seasons variable 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 range and second is the stop parameter if you don't pass in the start it starts with zero this is fine by us you will see in a second why and the stop parameter is the 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 so if you're running the file python 3 range underscore lan.pi then you will see that count starts with 0 1 2 3 and now 2 also gets 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 then square brackets and count you will get the index in each step which starts with zero and then one two and three it's running again to see what it is so the zero index of seasons is spring then summer fall and winter this looks almost as you want it to be but you have a problem here you are starting with zero so you have to increase the count variable for the print statement so it starts with one 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 dot pi example and the range len dot pi 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 dot pi 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 print statement then you must not forget to increment the count variable and more so you have to increment it after the print statement if you would increment it before the print statement then you would start with two spring three summer and so on so you have to look in which order your statements are the second script you created the range underscore len dot pi also has an issue and if you look at it just with this slide it might be compared to the first one already very visible that it's harder to read there is this this one line with all the parentheses range lens 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 a 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 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 this only works with a list in this case so let's see how enumerate fixes those shortcomings 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 enumerate dot pi 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 safe yet so let's save it and start with this creatively named file to explore the enumerate 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 created 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 enumerate and there you pass in the seasons variable then you can print the count and to season immediately without accessing some variables outside of the loop or using the range so i saved it let's run it and we're almost there again we have the zero index at the beginning but apart from this this looks exactly like the output that you aiming for so zero spring one summer to fall through winter should actually be one spring to 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 to 1 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 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 for loop so count and season are accessed in the print statement and you just create them with the enumerate function 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 rangeland example that you have to access the index of a list inside of the for loop 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