Discover how Python lists store multiple values efficiently and explore advanced indexing and slicing techniques. Enhance your coding skills by understanding list manipulation, including nested lists and slicing operations.
Key Insights
- Understand Python lists as collections of multiple values enclosed in square brackets and indexed numerically, with indices starting at 0 and the last item accessible using -1.
- Master the slicing technique in Python, using start and end indices separated by a colon to select ranges from lists and strings, remembering that the end index is exclusive.
- Explore nested lists (lists within lists) and learn to access elements within them using multiple square brackets, utilizing the pprint module to display nested structures clearly.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Right now we're on to lists. That's another, so final giant data type of this lesson. A list stores more than one value in the same variable name.
So think of it like your grocery list or something. You've got eight things on it, milk, bread, oranges, whatever, but it's all just a list. List items go in square brackets.
That's how you know where they begin and end. And the items in the list are comma separated, so you know that they're separate items. And the items are stored in numeric value, a numeric order called the index, just like the string.
Remember apple zero is the A for apple. So the first item in a list is at index zero, same as with a string. The last item in a list is at index negative one, same as a string.
If you have pets and iguana is the last thing, it doesn't matter how many pets are here, iguana is going to be negative one. Right, you could add another one, ferret, and it's still going to be negative one. All right, so what we're going to do is declare a list of strings.
We're going to call it pets and just wrap these guys in square brackets, shortcut, select all, hit the opening square bracket. We're going to print pets, and then we're going to print the type of pets, which should be list, and let's print the length. Lists for len list returns number of items in list, right? If we say len pets, we should get a six.
There it is actually, right there. I'm going to take that out. I just wrote it there.
Run it, boom. It's a list, and it's got a length of six. The items go, and let's just recap, the items go in square brackets.
The individual items are comma separated. The list items are stored by numeric order index, we're going to get to that next. The first item, okay, we're going to get to that next.
A list is a vector. It's a 1D structure. That's a little more than we need to know right now.
That will be gotten into in data science. Starting with lesson six, we'll start to care about vectors and matrices and one versus two dimensional. So a list, just a bunch of items strung along here is considered a one dimensional structure, whereas like a spreadsheet of rows and columns, an Excel spreadsheet, that would be our table.
That would be a 2D structure, which is fundamental to data science and what is called a data frame, but that's lesson six, seven and beyond. Okay, so right now we've got this vector thing called pets, and what else can we do with it? Let's get these items. We want the bunny, the iguana, the dog, the hamster.
Now the bunny is the first pet, so we'll say pet zero. The iguana is the last one, so we'll say pets negative one. The dog is the third one, so we'll say pets two.
And the hamster is the next to the last one, so we'll say pets negative two. And we could change the value. Let's say change cat to canary.
Replace cat with canary. So the cat usually eats the canary, but the roles are reversed here. We're going to say pets one.
We're targeting index one. That's the cat. We're overriding whatever's there, right? Cat is getting erased basically and reset and replaced with the canary.
And there you go. Replace the cat with the canary. Add index.
If you want to replace the gerbil with a guinea pig, it would be zero, one, two, three. Pets three equals guinea pig. All right.
Now what about slicing? Starting and ending. I want to get multiple items, not just the one. All right.
So let's look at how to get the first three items. So you still use square brackets, but you have to pass in two values now. Start and end separated by a colon.
So the start index in the case of the first three items, we would say we want pets square bracket zero. That's where we're starting. Now if we just say zero, we get bunny, but we don't want just bunny.
We want to go. We put a colon and then we put our end index. And if we want dog, we could say, okay, the dog is at index two.
We're going to put a two and then run. And oops, we didn't get the dog because if you note here, it says the last index is exclusive. Okay.
If you want to get the dog, you have to go one beyond the dog to get the gerbil. So it'd be zero, one, two, three. So because the last index is exclusive, not included in the slice, you have to go one beyond it.
And there you go. There's your first three items. Now to get dog, gerbil, hamster, we would want to go starting at where we'll say pets.
That'd be a two, right? And we're going to go two, three, four. Remember the four would be exclusive. We have to say five.
There you go. The last three items. Now you could say for the last four items, you could say, you know, three and then go to six, which doesn't even exist.
And that would work or better. You would start at negative three, you count backwards. Remember we have the backward negative indexing.
We would go three from the back and then colon, and they just leave it blank. So list zero index gets from beginning to end index exclusive. So you wouldn't get this last index, right? We said zero, we did that, right? We said zero, three to get the items at zero, one, and two, right? The three wasn't included.
And we could say negative index to the end. That's going to get from start counting backwards to end of list. And you could say go to negative one, excluding last item.
If you start, you back up to negative five and you go to negative one. Remember the last one's exclusive. It wouldn't get you the last one.
We could also start at one and go to negative one, gets all but first and last. So let's try, get all but first and last items, right? We would say pets. I don't want zero, right? Because I don't want the first one.
I want one. And I don't want to just go to the end. One colon nothing would take you all the way to the end.
We want to go to the last one, which is exclusive. And there you go. So slicing.
We'll be doing a lot with this, but just a little taste to start. This is called slicing or selecting ranges. Let's say selecting ranges, slices of lists.
You can also do that with strings. You could slice, you know, get the beginning of a string, the middle part of a string. Lists or strings.
So these moves work on strings too. Let's try that. We'll say the above list slice operations all work on strings too.
We could say fruit equals pineapple. Pineapple. And we'll say, get the pine and the apple separately.
We'll say print. We're going to start. We want the pine.
We'll say fruit zero, start with the P. Now, if you want to start at zero, you can leave the zero out, but let's put the zero for clarity. And then we're going to go zero, one, two, three. That will get you pine.
We have to go one more. Remember the end is exclusive. So that would be a four and there's your pine.
And then for apple, you would start backwards negative five and go to the end. Okay. Little challenge.
Pause. Here, let's do a string. Get the dragon and fly separately.
We'll say insect equals dragonfly. All right. So pause and go ahead and do that.
All right. Here we are. We're going to say, all right.
So for the fly, for the dragon, we want to start at the beginning and I'm not going to put the zero. You don't have to. You're starting at zero.
Zero, one, two, three, four, five. We need to go one more. That'll be a six.
And then the fly, we just start at negative three and go to the end. Dragonfly. Oh, that's fruit.
Okay. Let's try one more. Challenge A. Challenge B. Let's try to get something in the middle.
Get the catch in flycatcher. I don't even know if that's one word. Let's call it a word.
It's a thing. Print. It's not really one word probably, but it's okay.
It doesn't matter. A thing. We're going to start at zero, one, two, three.
And we're going to go to negative two. Right? In this case, if you like. There it is.
Okay. Rolling. All right.
A nested list is a list inside a list. So let's call this pet groups. We have pets by, we've got some mammals and we've got ourselves, let's make it like nice and neat.
We've got a list of lists basically. Pet groups. Right? We've got them divided up into mammals, reptiles, and birds.
You could do fish if you like, but we get the concept. Okay. So what we're going to do is use double square brackets to access items.
Let's just print the whole thing to see what it looks like here. Okay. Now it's printing all the way across like that, which we don't really like.
So what we're going to do is, and we're going to be doing this a lot. Usually we're going to import something. So hit code and we're going to say import pprint as pp.
Pprint means pretty print, and it'll make it nice and pretty. We'll say, usually you have to do pp.pp. You need four p's in a row, but it's worth every p because look how nice that is. You don't have to scroll sideways.
All right. We're going to use double square brackets to access items from the nested list. If we want to get this child list out of the parent list, it's really just item zero, right? Because there's only three items in here.
If we print the length of this, we're getting a three, right? There's only three things in there. We're going to print pet groups zero, and that'll be the entire child list. And if we want the next one, we want the reptiles, that would be item one.
It's the second item, right? There's only three. All right. Now, what if you want to come back to the first item, but you don't want all the items in the first item? Well, let's get the first item.
That is zero. That would be bunny, cat, dog, gerbil, hamster. And then further, we're going to say we want to start at the beginning and go to three exclusive, and there's your cat, bunny, cat, dog.
And if you just want bunny, that's zero, zero, right? It's item zero at item zero, right? Item zero being the six-pack or the five-pack of mammals, and then bunny being the first mammal in mammals. Python would be the second item in the second item. So that's, notice we're using two sets.
We're first square bracket to get to the first child list, and then drilling into that list. For the individual item or items, and then parakeet, falcon, eagle, that's the last three items of the last item. So for the last item, you could say negative one or two, and then the last three, we could just say negative three to the end, and there you go.
Parakeet, falcon, eagle. Pause, type it out. That's for you to do, okay? I know I've said it a bunch of times, but I will continue to say it, not to henpeck or anything, but you do need to type this stuff.
If you type it, you will learn it really well, okay? So please. Okay, great.