[MUSIC] It is a very common operation to want to process every single element of an item in a list. Okay, so you want to loop over the entire list and do the same thing again and again, but once for each item. Maybe you want to print every item in a list. Well, the process of repeating something over and over in computer science is called iteration. Okay, we're going to create a loop and we're going to do the same thing for each element of the list. Maybe you want to to go over the list and print every element. Usually it's more complicated than that, but that gives you an idea. So in this video, we're going to look at how you can iterate over lists. Let's take a look at the function, print_items. The objective of this function is to print every item of the list, one item per line, okay? And so it's going to iterate over the list and print each item. So let's look at the syntax of a for loop, all right? Well for loop starts with the keyword for, as you can see. And then it has a variable name, and in this case that variable name is item and says in and then the list, alist. Okay, now in here is not the same as when we were checking to see if an item was in a list, okay? So Python is using the keyword in, in two different ways. When it's in a statement that begins with a the keyword for, it means for every item in a list. That's the way to read that statement there. And notice that it ends with a colon. And in Python, every time something ends in the colon it means, hey, after this there's going to be a block of code, and that block of code is going to be indented, all right? And so you can see the body of this loop is simply the statement that prints item. So what's going to happen here is, the loop is going to go through every single item of the list or every element of the list let's say, to avoid confusion. And each element will be assigned to the variable item for one execution of the body of the loop. So the first time through, item will be the list element at index zero, we will print it. The second time through it'll be the list element at index one, we will print it and so on. Okay, so this allows us to repeat a block of code. In this case it's just one line, print item, over and over and over again, where there's a variable, in this case it's called item, that holds each element of the list in turn, right? So let's try this out, okay? Let's go down here, okay? We have two lists. Numbers, which we create using range and converting that into a list. And then a list of strings that is just typed in here directly, okay? And I'm going to call print_item on numbers and print_item on strings. And let's see what happens. Okay, a whole bunch of numbers got printed, all right? And you should convince yourself that those are the numbers that should have been printed in that range. But if I look at the last several, it's 71, 74, 77, 80, they are increasing by 3 which is the step size in that range, and it does stop at 80. Okay, which if I added three more would be 83, which is greater than or equal to 82. So that does make sense. Notice that when I print the strings, I get Python is fun exclamation point, each on a separate line, okay? So again, let's go back up and look at how print_items is working, okay? For item in alist, let's think about the strings. First time through, item would get the string Python, all right? Then we'd execute the body, which is indented by four spaces after the colon, right? It says print item, so item's Python, so that gets printed. Then we go back up to the top again, and item gets the next element of alist, which is is. And then we print items, so we print out is and go back to up. Item gets fun, we print fun, back up. Item gets the exclamation point, we print it. Okay, so this is the basics of how we use for loops to iterate over lists. Now let's take a look at the next function of this file that I've called print_items_bad, right? This is also going to iterate over alist and print out all items. So it's going to do exactly the same thing, but it's going to do it in a different way. Instead of iterating directly over the list, we're going to iterate over a sequence of numbers and use those numbers as indices into the list. So if we look at the loop here it says for index in range length. Well, what's length? If we look above, we call len on alist to get the length of the list. So range length should be a sequence of numbers from zero up to the index of the last element in the list. And this is why range works this way, right? Why it doesn't include that last number, right? It's very convenient to use it for things like this. So I have for index in range length. This means that index will take on each of the elements of range, starting at zero. So if we had a linked list of length five, it would become zero the first time through. And we'd execute the body, then it would be one, and so on, right? All the way up to four and then we'd be done. Now I want you to notice something here, when I called a range this way I didn't have to convert it into alist. Range does allow you to iterate directly over the range object that it creates, without having to first turn it into alist, okay? Now notice how I print things now. I can't just print index, that's not what I want. I have to print alist sub index, okay? Now why is this bad? Well, it's not inherently bad but this is not a good way of iterating over a list, why? It's very error prone. What if I screw this up, okay? What if I had range length plus one, because I calculated the length in a different way. I could very easily not iterate over all of the indices of the list. l could go past in the end, l can miss indices inside, and so on. So if you're iterating over a list just to do something to issue the items, you should always use the first form in print items, okay? There are some times though when it does make sense to use range and iterate over a sequence of numbers, okay? But this is not one of them. It's when you actually need to use the index for something here. Notice all I'm using the index for is to get the item. Well, I could have just gotten the item directly. If I needed the index for something else, like I wanted to know where I was in the list, then this would make more sense, right? But let's run it anyway. Okay, and we can see, hopefully, then it does it exactly the same thing, all right? We now have the sequence of numbers that got printed out in the same way, all right? We just did it in a little bit more dangerous fashion, all right? So I always would recommend that you use the first way if you can, right? Iterate directly over the list, if that works, okay? Only resort to iterating over a range of indices if you are not able to write the code in the first way that we did inside of print items. Now let's imagine that we didn't have the len function, and I wanted to figure out how many elements were inside of a list. This function count_items here can do so, okay? So it takes alist as input and then it counts the number of items. So first I initialize the variable count to be 0. Then I iterate over all of the items in alist for item in alist. And I increment count by 1 for each item. Notice I don't ever even look at item. That's okay, so I assigned each value of the list to the variable named item. I completely ignore it, I just increment the count. And then when I'm done, I return that count. This is going to be the same as checking the length, right? So let's try that. And we run it, and we find out that my list of numbers has 26 items in it, and my list of strings has 4 items in it, okay? All right, that's not very interesting, okay? I didn't actually do anything with each item. And so here is a more interesting function. Count_odd_items, now this is not going to work if I pass it something that isn't numbers, so I now need a numlist. I want to count the number of odd items in numlist. So again, I initialize the count to 0 and then I say for num in numlist, right? That's going to iterate over all the elements of numlist and assign them to the variable num intern. And then the body of the loop now, I have a conditional, I say, if num % 2 == 1. Well, the percent operator is remainder, so I divide by 2 and take the remainder. If I divide by 2 and take the remainder, if it's 0, then I know the number was even. If it's 1, then I know the number was odd. So I'm checking if it's equal to 1, meaning that the number was odd. And in the body of the if, I increment the count, count plus equals 1, by 1, right? So now I'm going to iterate over all the items in the list. I'm actually going to look at the item and do something based upon the value of the item. And I'm going to increment the count for odd numbers and not for even numbers, and return, okay? So this is potentially the most interesting loop that we've looked at here in this lecture, right? It actually does something with the elements of the list. And returns me something based upon what was inside the list, okay? And that tells us there were 13 odd items in the list. And I strongly encourage you to actually go look at that list. Make sure there actually were 13 items, make sure you understand it. Maybe even change it around to count the number of even items in the list. In this video, we've seen how to use for loops to iterate over lists in Python. The for in syntax allows us to specify the name of a loop variable and the list over which we'd like to iterate. Then the loop body executes for each element of the list. And within the loop body you can access the current element through the name of the loop variable that you specified in the for statement. This is an extremely powerful concept. Now we only use relatively simple loop bodies in this video of a couple of lines, but there are no restrictions. You can write any Python code that you would like inside of the loop body. You can call their functions, you can print out things, and so on. This is a useful concept and we're going to see it again and again throughout the class.