This lesson is from the Real Python video course by Christopher Bailey.
Nesting
00:00 This video is about nesting within lists. So, you’ve already seen that an element in a list can be any sort of object. That means that a list could be one of those objects.
00:10 You can have a list within a list, and that list can also have a list within it—and so on, and so on, like in this example.
00:19
For the object x
, there’s three layers going on here. In index 0
, 2
, and 4
, there’s a simple first layer, but then in index 1
, there’s a second layer with its own indexes. And then in index 1
of that, x[1][1]
, there’s a third layer.
00:37 These sublists within the lists can be created pretty easily with additional square brackets.
00:45
Let me take you through this example. For this example, I’m going to have you recreate that list shown in the slides. At index 0
you start with the object 'a'
, and within that at index 1
, you start with a sublist with 'bb'
and then an additional sublist at its index 1
, closing that sublist after two items.
01:07 Then close the other sublist after four items. So now you’re back down to the first level.
01:22
Type in x
, you can see that it’s a complete list. So, can you index it? Sure! That first index would be 'a'
. In fact, I’ll show you all three together.
01:32
Here’s index 0
, index 2
, and index 4
.
01:37
So a
, g
, and j
. Now, index 1
is a sublist, and inside of it, it has another sublist. If you wanted to access that sublist, it’s done by adding another index set with another pair of square brackets.
01:53
So if you want to access 'ccc'
, that would be index x[1]
—well, here 0
is going to be 'bb'
. If you want to get to the second value here, that would be here. This is going to return both of these items. Okay, in fact, well, let’s go a little further. x[1][2]
is going to be 'ee'
, okay. But what if you wanted to just get to 'ddd'
here?
02:17
That’d be x[1][1][1]
. That’s taking the second index—again, everything’s zero-based, right? So here, grabbing this. Then by doing x[1][1]
, we got this. Now, if we want to get this guy, that’s one more index.
02:34
That’s going to get you 'ddd'
. Similarly, if you wanted to grab items in this sublist. Well, that’s way down here, so index 0
, 1
, 2
, 3
—x[3]
. And if you wanted to grab the first element, that would be [0]
. And negative indexing works here too.
02:53
So if you were to say x[-2]
, which would grab this guy here, and [-1]
—that should give me 'ii'
. Right. Versus x[-4]
and then [-3]
. And let’s say we want 'cc'
.
03:17
So again, here’s x
. What is the length of x
? How many objects does it have? It only has five objects: one, two, three, four, five.
03:27
So, this is a sublist, and this is a sublist, those are objects within it, and then this list even has another sublist within it also. But as far as objects go, there’s only five total in the main list of x
.
03:40
So if we’re practicing with the operators again, what if you were to say is the string 'ddd' in x
? Well, it says that’s False
, because this is only searching the first level.
03:52
Even if you were to say, “Okay, well, I know that it’s in x[1]
. It’s part of that. It’s a sublist of that.” Nope! It will not search inside of a sublist automatically.
04:03
You’d have to specify all the way inside here, so you’ve got to go inside of not only index 1
of this whole list—getting this object—but then inside this object here, which is, again, the second index. That would return True
because it’s a member of this list here. An individual element in a sublist doesn’t count as an element of the parent list.
04:26 It only counts as an element of its own list.
04:30 Next up, you’ll learn about how lists are mutable and dynamic.
You must own this product to join the conversation.