Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn about the enumerate function, another tool to iterate over sequences.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Cool, we've seen how basic for
loops work in Python.
0:00
We can iterate through a sequence and
0:02
access each element of that
sequence individually.
0:04
In this video,
we're gonna be expanding on the for
0:07
loop a little bit to talk
about the enumerate function.
0:10
The enumerate function comes in
handy when we need to know the index
0:13
of the current element in our loop.
0:16
Or we want some kind of counter.
0:18
Let's look at this grocery
list example again.
0:20
We have this list, and that's all well and
good, but I want it to print out nice, and
0:22
neat, and numbered.
0:26
How do I get the numbers?
0:27
Well, here's one potential option
that we're going to explore and
0:29
then improve on.
0:32
So, first we can create a variable
to store our counter number.
0:34
We'll assign the integer one to it
because most grocery lists start at one.
0:37
Then we have our for
loop here from the previous example.
0:46
But I'm gonna edit the print statement so
that it prints out the index variable too.
0:49
I'm gonna use an f-string to do this.
0:53
An f-string is something we can use
in Python to help us print variables
0:55
inside of strings.
0:59
To use one,
you type the letter f before your string.
1:01
Then when you want to insert a variable,
you simply include it inside your string,
1:09
surrounded by curly braces.
1:13
F-strings are new in Python three and
1:17
won't work if you're
running an earlier version.
1:19
So I'm gonna add the index
variable in here too.
1:21
So that print statement is printing
out the value of the index variable.
1:28
Followed by a period, then a space, and
1:32
then the value of our item variable,
which is the current element in the loop.
1:35
Then finally, I'll increase the value
of the index variable by one.
1:38
Now, if we run this we'll see a neat and
numbered grocery list.
1:47
Let's check.
1:50
Cool, this works, but
this approach is not very Pythonic.
1:59
Mostly because Python provides a built in
function that does this exact same thing
2:03
for us in a much cleaner way.
2:07
This function is called enumerate.
2:09
Enumerate takes two arguments,
an iterable and
2:11
an optional argument that defines
a starting counter number.
2:14
It returns what the Python
docs call an enumerate object.
2:17
You'll learn all about
objects in later material.
2:21
But for now just understand that, when you
call this Python function in a for loop,
2:23
you get back both the index
of the current element along
2:27
with the current element itself.
2:30
So, to use the enumerate function,
2:32
we'll replace the reference to
the groceries list with a call to
2:34
the enumerate function where we pass
the groceries list as an argument to it.
2:37
Now we'll change our item
variable in the loop to a tuple.
2:49
This is one of those handy spots
where we can use multiple assignment.
2:52
Finally we can get rid of the old
references to the index variable,
3:00
we don't need them anymore.
3:03
And that's it.
3:08
The current item in the grocery list
will be assigned to the item variable.
3:09
And its corresponding index will
be assigned to the index variable.
3:13
If we run this,
3:16
we'll see that we get a really similar
output to our first approach, let's check
3:17
Okay, it's pretty close but we don't
want a grocery list that starts at zero.
3:28
Sure, Python sequences
start with index zero, but
3:32
most lists written by humans don't.
3:34
The enumerate function accounts for this
with that optional argument I mentioned.
3:37
Let's pass a second argument to
the enumerate function, the number 1.
3:41
This tells enumerate to start the counter
at 1, instead of the default of 0.
3:45
Now if we save and run this again, we
should have the lists we're looking for.
3:53
Cool, looking good.
4:02
Now, just so you know, the optional
argument will take any valid integer.
4:03
If for some curious reason we wanted
the grocery list to start at 20,
4:08
we could do that too.
4:11
Let's check it out.
4:17
Cool, all right.
4:21
Great work, everyone.
4:22
See you in the next video.
4:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up