It's possible to iterate through lists in different ways. In this video, learn the advantages of different approaches of iterating through lists in Python.
- [Instructor] We're going to take a moment to talk about indices within a 2D list. Now you can see on the screen that we have a list containing five inner lists. So these represent the rows within our grid. And within each row we have some tuples containing pairs of coordinates. Now it's important that you understand the difference between the more common Cartesian coordinates that you might be used to from mathematics, and matrix coordinates, which are a part, generally, of more advanced mathematics. They are similar, and yet different. So, in Cartesian coordinates, we have an x-axis and a y-axis, x representing the horizontal direction, and y representing the vertical direction. In matrix coordinates, we have generally i and j are used, and i represents the row number, and j represents the column number. So j is equivalent to the x coordinate, but it comes second in the matrix coordinates, or the 2D list coordinates. Another difference is that on the vertical axis, that's the row number, as you increase the value, you're actually moving down the matrix, rather than up. So let's look at a couple of examples. If you look at the tuple at 4,2, you can see that that is in the row number four, and column number two. And again, because this is Python, we start counting at zero, and that will be consistent throughout the course, and throughout all of Python. So row number zero is, in fact, what we would normally think of as the first row. So again, just to emphasize that we often use i and j, i representing the row number, and j representing the column number, and this will be the case in our code. Okay, so this is what our code looked like at the end of our last video. And we had created some 2D lists, and appended some items. Now we're going to look at iteration. So there's two methods we're going to look at. The first one is parallel to the method we used for 1D lists, where we used the in keyword, and I'll show you what that looks like, "for row in list_2d". Now here we're going to use a construct called a nested for loop. We actually have a loop within a loop. I'll talk about that more in a minute. For item in row, we're going to print the item and we're going to add end equals an empty string. What this does in Python is it forces all characters to be output on the same line. And then back one level of indentation. We just print nothing to force the output onto a new line. Now, I'm going to clarify what's going on here. So, like with 1D lists, we have for the thing in the list. In this case it's a row. We're going to do something. For each of those, we're going to create another loop. So this is for every column within that list. We're then going to print the item in that position, that row, that column. We're going to stay on the same line until we get to the end of a row, in which case we want to put the output onto a new line. Now I'm going to run that. The first four lines of output are from the previous exercises that we did. So what we're interested in is the ABC DEF, which is the output from the code we just wrote for iteration. So that's one method. The next method we're going to look at parallels the second method in the 1D list version. We're going to use the range function to iterate. So we do, "for row_num". Now some people just write row, you often see that. I prefer to be explicit, and I encourage you to be the same. We're talking about the row number, we're not talking about the whole row. So for the row number in the range, and we're just going to hard code the value here because we know the size of our 2D list. You might do it dynamically in a different context. Now, the nested part, "for col_num in range 3". All right, so this is specific to our list that we've created between lines 16 and 21 here. We're going to print list 2d, and this is where these indices come in. So we got the row_num as our first index, and we've got our col_num as our second index. And then we're going to have the same, end equals the empty stream, to force the output from that to be on the same line. But at the end of each row we want to force the output onto a new line by an empty print statement. In fact, I don't want the empty string in this case. I want a space between my items, so I'm just going to change that. Let's run that. So you see all the previous output, and then the last two lines have now got a space between my characters as a result of that change that I made there. You can, in fact, have anything you want in there to delineate. You could have a plus sign, just for fun, so that's a useful part of the print function, if you didn't know about that. So in our project we're going to be using this version between 41 and 44, the second version, using the range function to iterate through a 2D list quite often, so make sure that you understand this before we move on. Okay, so I'm just going to change that back to a space now. This example here we just had an empty string, so let me just run that again so you can see the difference. So you can see, our final version had spaces between them, and our first version didn't, and that was all as a result of whatever was between these quotations on line 43, the end argument to the print statement. So that second way of doing things from line 41 to 44 is going to be important moving forward, because in our project, we're going to very often want to target very specific locations within a 2D list.