#100DaysOfCode in Python Transcripts
Chapter: Days 34-36: Refactoring / Pythonic code
Lecture: Refactoring 2: loop counting == enumerate

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up, counting inside a loop.
0:03 So sometimes you want to keep track of an index
0:06 when you loop over a sequence,
0:07 and when you come from C or another language
0:11 you would typically do something like this.
0:14 Let's define a list of days
0:16 and let's loop through them showing the day
0:19 prepended by the number.
0:26 Alright, so that's straightforward.
0:30 Now, and this is correct, right?
0:31 I mean you can do it like this,
0:33 it's all 100% correct.
0:35 But the more idiomatic or Pythonic way
0:38 is to use enumerate.
0:40 And a way to do it is to wrap your sequence in enumerate,
0:46 which returns the index and the item
0:48 in the sequence on every loop.
0:49 So I can just,
0:52 now,
0:53 print those,
0:57 and it should give me, oops,
0:58 obviously, I should not hard code days.
1:02 So I'm using f-strings by the way because I'm on
1:05 Python 3.6.
1:06 And yes, this gives me the same result,
1:08 and there's even a nice little trick with enumerate,
1:12 which is, you can give it a starting point.
1:14 So I can just copy this,
1:17 and again give enumerate a second argument of 1.
1:20 So I want to start the counter at 1,
1:23 and then I don't have to do this menu
1:25 plus 1 inside the loop.
1:26 And that gives me the same result.