Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Nest Your Lists

00:00 Quick check-in. You’ve already made great progress in this course. In the next lessons, you’re going to learn about nesting and copying lists. Let’s get started with that.

00:10 You’ve learned earlier that you can put any sort of data type inside of a list or a tuple, and that means you can also put other lists or other tuples inside of them, or mix and match.

00:21 What you can create, if you put a collection inside of a collection, is a nested collection. So you can nest lists and tuples. For example, here you can see a couple of Māori words.

00:32 You can see that there’s first the Māori word and then the English translation, and they’re each inside a single list. So you have these three: ["whānau", "family"], ["kai", "food"], and ["aroha", "love"], and each of those are inside of a list.

00:46 And then all of those are, again, nested inside of another list.

00:52 And then you can access these elements using double index notation. So with the first index, you’re going to access the list that’s nested inside of the list.

01:01 So in this case, you’re accessing the final list inside of this nested list structure. And then with the second index, in this case zero, you’re going to access the first element in here, which is "aroha".

01:13 And then down here, you do the same thing, but you access the first element of the nested list, which is going to be the English translation "love". Let’s try it out in IDLE.

01:25 So here’s that same list that you saw on the slides, and now you can see that this is just a normal list, but it’s nested. So you can see there’s double square brackets at the beginning.

01:34 First, it opens up the main list, and then it opens up the first element, which is also a list. Let’s go ahead and access that first element. I’m going to write maori_words and then open up square brackets and then put in index zero.

01:49 So this is going to give me the first element of the maori_words list, which is another list which contains first the Māori word "whānau", and then the English translation, "family".

02:00 And you can do this, of course, with the other pairs as well. So you could access the second element in the list by using the index one, et cetera. And then you can also go in and go for the translations.

02:14 You can continue to drill down. So I could say maori_words[1] and then I want to get first the Māori word, so I would open up another square bracket right after the first square brackets that I did, and then pass in the index of that element that I want to access.

02:32 So here I want to access the first element in the nested list, which is the Māori word for food.

02:38 And here I can get it as the output.

02:42 You can access the other element in there by using its index position.

02:46 I think that the best way to think about these nested collections is that you’re drilling down when you’re accessing elements. The first step, you drill down to the initial element that you want to see.

02:57 So in this case, with index one, you access the list in the middle of these three items. And then again, you use the index position to get the second element in that nested list.

03:12 Okay? So you can nest lists as deeply as you want to, but don’t do it unless you have a good reason to.

03:20 And that’s all about nesting lists. In the next lesson, you’re going to look at how you can copy lists and a few common gotchas that you can run into when you do that.

Become a Member to join the conversation.