Let me show you another useful function that will use just like ranch.
Mind you, it's not as common as ranch range.
You'll see it a lot in python code, but this other one is still very useful and it's called enumerate.
Now what does it do?
Let's have a look.
I can do a four loop and I'll say four and we'll say character in enumerate and then enumerate.
We wrap around something that, well, we want to enumerate, so let's see what that means.
It returns an enumerate to object.
So Iterable must be an other object that supports iteration.
So we have to give it something that enumerate can iterate over.
So this time around let's just do a a string that says hello.
Now enumerate is special because what it's going to give us is it's going to take an iterable object
and gives you an index counter and the item that index.
What do I mean by that?
Well, you can actually say I comma character.
So we're unpacking this and we're saying, hey, print I and character if I click run.
Look at that.
I get.
Hello.
Iterated over again.
If we don't have enumerate, then we would have just gotten.
Hello.
But we also.
Because we use enumerate, we get this eye or index of each item and where it is in the index.
So that if I had a list here, let's say one, two, three.
Again, I'm able to use and access the index number of the item as well.
What if we had a tuple?
Again, we get the index number.
So enumerate is very useful if you need the index counter of the item that you're looping through.
So let's do a little exercise.
Let's say that we want to create a script that enumerates a list of numbers one through ten.
So we'll use range to create that.
I'm going to say list and then I'm going to say range.
I know there's a lot of functions here, range that is 100 actually.
Let's do that.
And I want to be told what the index of the number 50 is.
So pause the video if you need to do that.
Now, here, I want it so that let me make this a little bit bigger.
I want it so that we only print the index of, let's say, the value 50.
I want to know where 50 is in the list.
Now again, this is a simple example, so you can probably already guess what the answer is, but how
would you code this so that only in here we get the index of 50 or it says the index of 50 is whatever.
Pause the video if you want to give it a go.
Otherwise, let's try this.
I'm going to do a conditional and says if character is equal to 50.
Then I'm going to print.
Index or I'm going to do an F string and I'll say that the index of 50 is and then I'll add the index
in here.
So that if I run.
I get an index of 50 is 50.
And just to confirm if, for example, I just print here and let's say print I in character, I click
run.
We see that?
Yep.
We have the indexes the same as the numbers.
Again, not too difficult of a question, but you see here that we're able to extract this information
again, not as useful as range, but you will see it occasionally in code.
So I wanted to make sure that you're not surprised by it.
As you can see, it's not that difficult.