One-dimensional lists are a fundamental data structure in Python which you need to understand to successfully work with 2-dimensional lists. In this video, learn how to properly implement and use 1D lists.
- [Instructor] This is going to be just a quick review of 1D lists in Python. Being familiar and comfortable with 1D lists is a necessary prerequisite for working with 2D lists. Let's dive in. So we're going to start by creating a list, and we're going to call it My List. And it's going to contain cat, which is a string, two, which is an integer, 4.5, which is a float, and false, which is a Boolean. So this might surprise some of you, if you're used to working with arrays, that we actually have here mixed data types, string, integer, float, and Boolean, whereas arrays normally just contain one data type in other languages. Now we can print the list simply as a whole by printing My List. And I'm going to run that so you can see it. The output is simply the list as it's been entered. However, we very often want to target a particular item within a list, and this is where indices come in. So the way we target a particular item within a list is we write the list name, and then using square brackets we have a numerical index for the position. Now we start counting from zero, so if I was to print My List one, that would actually print the second one in the list, or the item with index one. Going to run that now, and it's output two because two was in the position with index one. So that's how you target specific items within a list. Now one tip for you moving forward is that if you can always be really clear about the distinction between the index, that's the position within a list, and the actual value at that index, this could save you a lot of confusion. The next thing we're going to do is we're going to iterate through a list. Now that means to visit very item in a list in turn. We're going to look at two methods of doing this. This is the first one. For item in my list. Okay, so that's like saying for each item in my list. We print that item. Okay, so the name item is being assigned in turn to each item in the list. Just going to comment this out for now on line seven so it doesn't print. So now when I run the code, you can see the items of the list printed out in turn. Cat, two, 4.5, and false. Now it doesn't have to be item here. You can of course use a different variable name, such as elem for element, but you have to make sure that the variable names match. So on line nine, we have elem, and on line 10 at the moment we have item. So they don't match, so there'll be a problem. You need to make them match. So now when I run it, we get exactly the same as before. So that's the first method of iterating through a list. And in this case, what our iteration is achieving is simply outputting or printing the contents of the list. You might well want to do other things. But for now we're sticking with printing. The second method, I'll mark this out, involves using the range function in Python like this. For I in a range, and the range is the length of the list. 'Kay, so for I in range of My List, len of My List, actually it's a very common mistake people make is they don't put in len. I do it myself sometimes when I'm not thinking. I just put I in range My List, which isn't going to work. So make sure you actually have, inside the range function you have the length of My List. Then you print My List, and then in square brackets, the position of the item in the list which you want to print. Let's run that, and you can see again that we have cat, two, 4.5, and false all printed out in turn. Now there's a lot of situations where you might want to have explicit access to the index. So this is a good method in those situations. Later on in our game for example, we might want to know a particular position inside the game board, and in that case we need to know the values of the different indices involved. One more thing I want to look at is appending to a list, which means adding items to the end of a list so we can do My List dot append. And then the item you want to add, for example dog. So that will append to the end of the list the new value, so you can see now if we print My List and we run it, the list now contains the extra element at the end, dog, which we just appended to it. This is a very useful method that you'll definitely need later on in the project. So there we have a very quick review of 1D lists before we move onto the more complex 2D version.