Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
For loops in Python are very useful for iterating over container types, and yet they are significantly different from for loops in other languages. In particular, for loops in other languages tend to have some sort of an index variable associated with them, and this can some times be useful. And we don't have that in the standard for loop in Python. So, let's take a look at how we can get that very easily using the enumerate function. Go ahead and make a working copy of for.py. I'll call it for-working.py and open that working file, and this is our little readlines example. And if we run this, You'll notice that we get the lines of text from the lines.txt file, which looks like this.
They have that extra new line in them. We'll go ahead and get rid of that with end=" ". I am going to add something to this here. I am going to add the enumerate function, enumerate, and it goes like that. Enumerate actually returns two values. The first value is an index, and the second the value is the value from the iterator. I am going to print the index before the line, and we'll save this and run it, and you'll see what we get.
So, the index is this extra number over here, and you'll notice that it starts at 0, and that's actually a useful place for it because that's how you index containers in Python. So, let's take a look at an entirely different example here, and we'll take a string, and we'll iterate over that string. We'll use the enumerate function, and we will print the index and the character, just so you can get an idea of that looks like, save it and run it.
So, here we have - I am going to scroll back up - each position in the string, with its index number and its character. Now, let's say that I am just interested in the letter S. So, I want to know which of these characters are the letter S. I can say if c='s' like that: print ('index'), number is an S, and I'll do a format I, like this. That will place the value of the index into this placeholder here.
So, I'll save that, and run it, and now we know index 3 is an s, index 6 is an s and index 10 is an s. So, counting from 0, 1, 2, 3 there is an S, 4, 5, 6 is an S, 7, 8, 9 and 10. So 3, 6 and 10 are the Ss. So, sometime it's necessary to be able to find at what index, in an array type, a particular value is. This is a very useful pattern for doing that, and there is actually a lot of uses for having this index, and enumerate is how you can get that.
So, this is how you get indexes and an iterator with for loop. Use this pattern here for, index and value and enumerate, which returns those two values, and the argument to enumerate is the iterator. Then you have the index and the value available inside your for loop.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101414 Viewers
61 Video lessons · 88175 Viewers
71 Video lessons · 72028 Viewers
56 Video lessons · 103820 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.