4.3 Create and manipulate lists - Video Tutorials & Practice Problems
Video duration:
13m
Play a video:
<v ->So now, before getting into for loops,</v> we're actually gonna look at lists because for loops are super useful when used in conjunction with lists. And what is a list? Well, you know, you could have this list of top 20 movies of 2018. This is just some user, this is not my personal opinion, though a lot of these movies are great. So you have this list, and there's an order to it. You know? You've got the first item and the last item. As you go on through the year and you watch more movies, maybe you want to add something in, take something out, change the order of stuff, and you can do all of these things with the data type called lists that Python comes with. So for my list example, I found these lists of most livable cities. The one on the left is from 2017 and the one on the right is from 2018. I can create a new list by giving it a name, and then, if I use these square brackets, that will make an empty list. And I can check the type of cities. And, oh, I have to print it out. And it gives me a class list. And I can initialize it with some default data. This was Melbourne, Vienna, and Vancouver. So I can now print cities. Great. So if I wanted to say, "Print the top most livable cities," and then I want to give it a length, so the length of the list. And then print just cities. Okay, and I'll say in 2017. Okay, so I can get the length of the list, and I can get the full list. Right? And then I can also do other things to this list, like get an individual item. So if I wanted to get an item, I could use the square brackets again and give it a number, and this number will be the index of the item that it retrieves for me. So we start at zero instead of one, and that's very common in computer languages for the indexes to start at zero, because we don't want to waste any bits. And so this is zero, this is one, and this is two. Great, and then what happens if we put, oops, three in here? Well, it gives me an error saying, "Index error: list index is out of range." So be careful not to, so you might want to, like, you know, make sure that the index you're passing in is less than the length of the list. You can also put a negative number in. What does that do? And it gives me Vancouver, and it actually starts at zero, goes up, but also then it, like, wraps around and this is negative one. This is negative two. This is negative three. Okay, and then I'm gonna add a few more cities in here, Toronto, Calgary. Toronto and Calgary, and we are going to get, let's see. If we start at zero and then put a colon and then go to three, it'll give us zero, one, two, and just give us that subset of list. I can go to one to four, for example, and it'll give me one, two, and three. So you can see that it doesn't include this last number. It does include the first number and doesn't include the last number. That's the thing to remember when you're working with accessors and getting subsets of lists. We can say take everything up until four by not putting in a number from the very beginning and so it'll go zero to three, zero, one, two, three. And similarly we can not include a number at the very end and it'll give us items until there's none left. So from zero, one, two, three. It'll give us Toronto and Calgary. We can do this but this is not very useful since it gives us the full list. And what happens if I start it outside of the range? It just gives me an empty list. Okay, and then what happens if I do this? If I put another colon in here? Then this last number is gonna tell me how many numbers to increase each index by. So if I put two in here, it'll give me every second value. So Melbourne, Vancouver and Calgary. And this number can be anything, well as long as it was an integer. And it can also handle negative numbers. So this will start at the end, at negative one, and then decrease by one every time. So those are some things you can do with lists. You can also add an item to the list. So you could say cities.append. And that'll add an item to the end of the list. And the next item to add is Adelaide. And so this returns none because now cities is just... Append just changes cities but it doesn't return anything. So I can do that and now Adelaide is at the end. It's giving me this error because it's saying that I could've put it in there right away. And I can also remove items. Let's say I wanna remove Vancouver on there for the next addition because the rents are too high. And now it's gone, okay? I can also pop an item off and that pops the last item off of the list. So append and pop are like opposites. And then you can see that there's a lot more stuff in here. We can sort it. And now it's sorted by alphabetical order. And this is actually using the comparison, the less than and greater than comparison that we saw in an earlier class. It'll do that to each item, to each pair of items. And then sort them based on that. And I just wanna show you a quick keyboard shortcut in PyCharm which can be helpful. If you press command or control, depending on what operating system you're on, and click on this sort, you'll actually be able to see some stubs and some documentation about what each of these things does. If you're trying to figure out, oh I don't remember the name of this method and I know that you can do it, or I wanna see all the different methods that you can use, instead of going straight to the documentation on the website, you can also do this. Another way of getting the documentation is you can use this help function. And if I run that, it'll actually give all of that documentation and print it out to the console as well so you can see the different methods there are. If you're curious about what these underscore methods are, these are actually like the private ones, and this is the function that gets called when you use the not equal to symbol or the length function. They're called Dunder methods. They're more advanced thing. But if you wanna get into that, you can look it up. Or now you know what to look up. You can also change the value of an item. Like maybe I spelled Vienna wrong originally, I can get the index, so zero, one. And then change it to what I want it to be. And now that is changed. I can give it a totally different one, Osaka, and now that value is Osaka. I can also insert values at a certain index. Oh, it's telling me that I put them in the wrong order. I wanna insert at this index. This value. Great. And so keeping in with Python's dynamically tight stuff, I can actually insert things that are not even strings. I can insert true. I can insert another list. And I can even have another list inside of here. So lists inside of lists, you might be like why would you do that? But if you wanted to model a matrix or if you're making a board game that is two by two, you can use that to identify the different spaces. Yeah, and that's it for lists right now. And in the future, we'll look at how to loop over each item. To recap, there are some properties of lists. They're ordered. The one at the first position, index zero, is always gonna stay there unless you change it and it just keeps its order. And then you can also have duplicate values. We didn't look at that but you can have an entire list that's just Vienna, because Vienna is amazing. I don't know, I haven't been there. But I've heard its amazing. So that's about lists and there are other kinds of what are called data structures that have different properties than this. And we'll look at some of those in lesson five.