2D lists build upon the concept of a 1D list. In this video, learn why two-dimensional lists are an important data structure in Python programming.
- [Instructor] 2d lists are a very important data structure used in many different applications. A good way to think of a 2d list is as a list of lists. Let's have a look at how that works. The first thing I'm going to do is to create an empty list. Call it list 2d and I use two square brackets with nothing in between them, to indicate an empty list. Now I'm going to append some items, and what I'm actually going to append are more empty lists. You'll see how that works in a moment. So list 2d, go to append, and I append the empty list. And I do that twice. So let's see what that looks like right now if we print it. Print, I'll run that. And you can see we have here a list containing two empty lists, as expected. So now let's add some items to the inner list. So we're going to do list 2d. And we're going to append to the first of them, which is the one with the index zero. And we're going to append the letter uppercase A and I'm going to save myself some work here by duplicating this line a few times. So the first three of these append statements are going to be appending to the first list, that's the one with index zero, and I'm going to append A, B, and C. And then the next three statements are going to be appending to the second list, or the one with index one. And we're going to be appending D, E and F. So now when we print it, and run, you can see that we have an outer list which contains two inner lists. The first inner list contains A, B, and C, the strings, and the second list contains D, E and F. So we have a list of lists. Now accessing specific elements, this is really key to this whole course. The ability to access specific positions within a 2d list is going to be used again and again. So pay close attention. If I do print list 2d, I need two indices, not just one. The first one refers to the position of the list within the containing list. So zero, that means the first list within my list. And the second index refers to the position within that inner list. So list 2d zero one will print B. Now take a moment to see if you can understand why that's true. List 2d zero refers to the whole list containing A, B, and C, which is highlighted on the screen now. And then the one part that means the second element in that list. So the second item in that first list is the character B. Now this might seem confusing at first, and it takes a bit of time to get used to how to index into a 2d list. So be patient with yourself. Let's say we wanted to print the character D. What do you think we would do? Perhaps spend a moment thinking about it, and then come back if you like. If you're ready, then let's look, print list 2d. So we want the character D that's going to be in the second inner list, that's the one with index one. And it's going to be at the zeroth position or the first position, because we start counting at zero. So let's run that. And you'll see that in fact, D has been printed. And as we said, it's in the second of the inner list, and it's in the first position of that second inner list.