Accessing List Objects
- 04:19
How to access and manipulate objects within lists in Python, using indices and 0 indexing for list manipulation.
Downloads
No associated resources to download.
Glossary
0 indexing indivdes list PythonTranscript
Okay, so now that we know how to create our list, the next question is how do we access and use the objects in those lists? So if we were doing it in Microsoft Excel, you know that we would create a reference. So if I wanted to take this four and add it to this five, I would create a reference between cells B6 and B7. And that would give me nine. So I would say equals this plus this, and it gives me nine very simple and easy to access. And you'll learn. Python is very simple and easy to access too. You just do it in a slightly different way. In Python, we access these objects using what's called an index. And importantly in Python, our lists are what's called zero indexed, and that just means that the first object in the list is correlates with zero. The second object in the list correlates with 1 and so on. So here if, if I want to use this four and this five, I want to add them together to get nine. That 4 is going to be list 1 index 3, it's the fourth object. So I go 0, 1, 2, 3. And then to get the five, I'm gonna go list 1, 4, 0, 1, 2, 3, 4 to add those together. So if I type list 1, and then in square brackets I type the index 3, I'm gonna get 4. If I type list 1, 4, it's gonna give me five.
So if I wanna add those together, all I have to do is say list 1, index 3, plus list 1, index 4, And I get 9 just like Excel. In addition to selecting single objects when then a list, you can also select a range. Let's look at how you do that. So if I wanted to select the first three objects in this list, 1, 2, 3, I would do it like this. I'd write the name of the list, list 1, and then in square brackets first I begin with the index of the first number. So I'm starting with 1. So I'm gonna use index zero, and I want to end with 3. So I do a colon that shows me that I'm selecting a range, and then I use the index of the last number that I want to exclude. So whatever the index is, the second index in my range, that number will be excluded. So I want to select 1, 2, and 3, and I'm going to exclude 4. The index for 4 is, So when I print out that range, I get 1, 2, and 3, Now if I'm starting my range at the beginning of the list, like I am here with the first object 1, I actually don't need to include that zero index, I can just write list 1, open, square, bracket. I can leave that first index blank, start with the colon, and then the index of the last number that I want to exclude. And I'm gonna get the same exact thing. Or alternatively, let's say I wanted to select 3, 4, and 5. So the index for those are 0, 1, 2, 3, and 4. So I'm gonna write list 1, I'm gonna open with 2, so that's the number 3. And I could put five if I wanted to. That's fine. And I'm gonna get 3, 4, 5, because five is the index for what would be six if that list continued. Or I could just write list 1, start with the index 2. That's the beginning of my range. And since I'm ending with the final object in the list, I can just leave that blank, close my square bracket, and I'm gonna get the same exact output.