1 00:00:00,230 --> 00:00:02,871 Cool, we've seen how basic for loops work in Python. 2 00:00:02,871 --> 00:00:04,569 We can iterate through a sequence and 3 00:00:04,569 --> 00:00:07,840 access each element of that sequence individually. 4 00:00:07,840 --> 00:00:10,080 In this video, we're gonna be expanding on the for 5 00:00:10,080 --> 00:00:12,410 loop a little bit to talk about the enumerate function. 6 00:00:13,410 --> 00:00:16,720 The enumerate function comes in handy when we need to know the index 7 00:00:16,720 --> 00:00:18,520 of the current element in our loop. 8 00:00:18,520 --> 00:00:20,210 Or we want some kind of counter. 9 00:00:20,210 --> 00:00:22,460 Let's look at this grocery list example again. 10 00:00:22,460 --> 00:00:26,340 We have this list, and that's all well and good, but I want it to print out nice, and 11 00:00:26,340 --> 00:00:27,720 neat, and numbered. 12 00:00:27,720 --> 00:00:29,310 How do I get the numbers? 13 00:00:29,310 --> 00:00:32,520 Well, here's one potential option that we're going to explore and 14 00:00:32,520 --> 00:00:34,150 then improve on. 15 00:00:34,150 --> 00:00:37,950 So, first we can create a variable to store our counter number. 16 00:00:37,950 --> 00:00:41,606 We'll assign the integer one to it because most grocery lists start at one. 17 00:00:46,235 --> 00:00:49,130 Then we have our for loop here from the previous example. 18 00:00:49,130 --> 00:00:53,530 But I'm gonna edit the print statement so that it prints out the index variable too. 19 00:00:53,530 --> 00:00:55,922 I'm gonna use an f-string to do this. 20 00:00:55,922 --> 00:00:59,520 An f-string is something we can use in Python to help us print variables 21 00:00:59,520 --> 00:01:01,220 inside of strings. 22 00:01:01,220 --> 00:01:04,546 To use one, you type the letter f before your string. 23 00:01:09,834 --> 00:01:13,865 Then when you want to insert a variable, you simply include it inside your string, 24 00:01:13,865 --> 00:01:15,313 surrounded by curly braces. 25 00:01:17,528 --> 00:01:19,009 F-strings are new in Python three and 26 00:01:19,009 --> 00:01:21,930 won't work if you're running an earlier version. 27 00:01:21,930 --> 00:01:23,781 So I'm gonna add the index variable in here too. 28 00:01:28,958 --> 00:01:32,810 So that print statement is printing out the value of the index variable. 29 00:01:32,810 --> 00:01:35,050 Followed by a period, then a space, and 30 00:01:35,050 --> 00:01:38,830 then the value of our item variable, which is the current element in the loop. 31 00:01:38,830 --> 00:01:42,901 Then finally, I'll increase the value of the index variable by one. 32 00:01:47,245 --> 00:01:50,640 Now, if we run this we'll see a neat and numbered grocery list. 33 00:01:50,640 --> 00:01:51,371 Let's check. 34 00:01:59,468 --> 00:02:03,305 Cool, this works, but this approach is not very Pythonic. 35 00:02:03,305 --> 00:02:07,261 Mostly because Python provides a built in function that does this exact same thing 36 00:02:07,261 --> 00:02:09,270 for us in a much cleaner way. 37 00:02:09,270 --> 00:02:11,680 This function is called enumerate. 38 00:02:11,680 --> 00:02:14,580 Enumerate takes two arguments, an iterable and 39 00:02:14,580 --> 00:02:17,780 an optional argument that defines a starting counter number. 40 00:02:17,780 --> 00:02:21,190 It returns what the Python docs call an enumerate object. 41 00:02:21,190 --> 00:02:23,179 You'll learn all about objects in later material. 42 00:02:23,179 --> 00:02:27,390 But for now just understand that, when you call this Python function in a for loop, 43 00:02:27,390 --> 00:02:30,156 you get back both the index of the current element along 44 00:02:30,156 --> 00:02:32,790 with the current element itself. 45 00:02:32,790 --> 00:02:34,673 So, to use the enumerate function, 46 00:02:34,673 --> 00:02:37,955 we'll replace the reference to the groceries list with a call to 47 00:02:37,955 --> 00:02:41,925 the enumerate function where we pass the groceries list as an argument to it. 48 00:02:49,190 --> 00:02:52,429 Now we'll change our item variable in the loop to a tuple. 49 00:02:52,429 --> 00:02:55,567 This is one of those handy spots where we can use multiple assignment. 50 00:03:00,737 --> 00:03:03,767 Finally we can get rid of the old references to the index variable, 51 00:03:03,767 --> 00:03:05,134 we don't need them anymore. 52 00:03:08,964 --> 00:03:09,980 And that's it. 53 00:03:09,980 --> 00:03:13,180 The current item in the grocery list will be assigned to the item variable. 54 00:03:13,180 --> 00:03:16,690 And its corresponding index will be assigned to the index variable. 55 00:03:16,690 --> 00:03:17,446 If we run this, 56 00:03:17,446 --> 00:03:21,477 we'll see that we get a really similar output to our first approach, let's check 57 00:03:28,054 --> 00:03:32,030 Okay, it's pretty close but we don't want a grocery list that starts at zero. 58 00:03:32,030 --> 00:03:34,610 Sure, Python sequences start with index zero, but 59 00:03:34,610 --> 00:03:37,300 most lists written by humans don't. 60 00:03:37,300 --> 00:03:41,800 The enumerate function accounts for this with that optional argument I mentioned. 61 00:03:41,800 --> 00:03:45,365 Let's pass a second argument to the enumerate function, the number 1. 62 00:03:45,365 --> 00:03:49,639 This tells enumerate to start the counter at 1, instead of the default of 0. 63 00:03:53,997 --> 00:03:58,048 Now if we save and run this again, we should have the lists we're looking for. 64 00:04:02,125 --> 00:04:03,750 Cool, looking good. 65 00:04:03,750 --> 00:04:08,020 Now, just so you know, the optional argument will take any valid integer. 66 00:04:08,020 --> 00:04:11,151 If for some curious reason we wanted the grocery list to start at 20, 67 00:04:11,151 --> 00:04:12,116 we could do that too. 68 00:04:17,529 --> 00:04:18,553 Let's check it out. 69 00:04:21,401 --> 00:04:22,990 Cool, all right. 70 00:04:22,990 --> 00:04:23,870 Great work, everyone. 71 00:04:23,870 --> 00:04:24,620 See you in the next video.