Locked lesson.
About this lesson
We'll look at multidimensional lists, or - lists of lists.
Exercise files
Download this lesson’s related exercise files.
Lists of Lists.docx58.9 KB Lists of Lists - Solution.docx
58.9 KB
Quick reference
Lists of Lists
A list can contain another list.
When to use
If you need to store a list, inside of another list; this is how you do it.
Instructions
Lists can contain all sorts of data...including other lists. There are two main ways to do it, directly and indirectly.
Directly:
my_list = ["John", "Tim", "Mary, [1,2,3,4]]
Indirectly:
second_list = [1,2,3,4]
my_list = ["John", "Tim", "Mary, second_list]
You can access items from the second array like this:
print(my_list[3][1])
Hints & tips
- Lists can contain other lists
- Enter them directly (just type them in) or indirectly (reference them by list name)
- 00:04 In the last video, we looked at lists.
- 00:06 In this video, I want to look at lists of lists, or in other programming languages,
- 00:10 it would be called multi-dimensional arrays.
- 00:13 But this is Python, so we're going to call it a list of a list.
- 00:15 So we looked in the last video, let's get rid of this.
- 00:19 We looked how to create a list, how to add things, remove things, update things,
- 00:23 pretty simple.
- 00:23 But you'll notice, we have just strings here.
- 00:26 And when you have a list, you can put all kinds of different things in here.
- 00:29 So you could put numbers, for instance, 2, whatever.
- 00:33 So here, we can go print(first_names[]),
- 00:37 and then let's go, 0, 1, 2, 3, on the third item.
- 00:42 Save this and run it, we get 41.
- 00:45 Now, this is a number, and all the number-y type things that you can normally
- 00:49 do with numbers, you can continue to do.
- 00:51 So, we can go +1, save this, it's 42.
- 00:55 We could go first_names + first_names[4].
- 01:01 So we're going to add this one to this one, save,
- 01:04 becomes 43, so that's super cool.
- 01:08 Let's go, 0, 41 + John,
- 01:10 what do you think's going to happen here, big, fat error.
- 01:14 Cannot add a number and a string together, because they're two different data types,
- 01:18 so be sure and keep that in mind.
- 01:20 So we know that you can put all kinds of different things in a list.
- 01:24 We can put strings or we can put numbers, we can actually put variables as well.
- 01:28 So let's go, bob equals the name Bob.
- 01:33 Or we can change this to just bob, right, just put the variable itself in there.
- 01:39 So if we save this, 0, 1, 2, 3, and run it, we get Bob.
- 01:44 So that's pretty cool, but
- 01:45 the main thing I want to talk about in this video is lists of lists.
- 01:49 So we could put strings, we could put variables, we could put numbers,
- 01:52 we can also put other lists inside of our list.
- 01:55 And we could do this a couple of different ways,
- 01:56 we're going to look at both of them in this video.
- 01:58 First, you can just put in two more square bracket, and
- 02:02 then enter stuff inside of here, right?
- 02:05 So this is a list, it's inside of our list, it becomes an item of the list.
- 02:12 So the 0, 1, 2,
- 02:14 3rd item in our list is another list.
- 02:19 So 0, 1, 2, 3, and 3 here, if we save this,
- 02:23 it's going to print out our embedded list, our list inside of our list.
- 02:27 So that is really, really cool, but you're not often going to want to just print out
- 02:32 the second list, the second array.
- 02:34 You're going to want to pull out an item from that list, so how do you do that?
- 02:38 Well, let's say we want to pull out the number 2,
- 02:42 this is the 0, 1, the 1th item in this list.
- 02:45 So what we do is, we just stick another set of square brackets on there,
- 02:50 1, boom, save it.
- 02:52 And hopefully it'll print out 2, and there it is, 2.
- 02:54 And again, since these are numbers, you can do all the same number-y type things,
- 02:59 let's go, 3.
- 03:00 So now it should print out 2 + 3, which is 5, and there you go.
- 03:03 So super, super cool, so that's one way to add a list inside of a list.
- 03:08 We can also, let's set our lists separately, so second_ list =, boom.
- 03:14 So now, we've created another list, and it's right here.
- 03:18 Instead of printing this whole big thing inside of our list,
- 03:21 we can just put the name, just like we did the variable, just a minute ago.
- 03:25 So if we save this, again, it should be 2,
- 03:28 because we're calling the third item, 0, 1, 2, 3.
- 03:33 And the first item in that thing, which is 0, 1, which is 2, and there we have 2.
- 03:39 So that's how you do a list inside of a list.
- 03:42 If you're coming from another programming language,
- 03:44 it's called a multi-dimensional array.
- 03:46 Whatever you call it doesn't really matter, super useful, you'll use this.
- 03:49 You can just put the list inside on its own.
- 03:51 Or you can declare it outside and
- 03:53 then just use the variable name inside your main list like that, very cool.
- 03:58 So those are lists, very useful.
- 03:59 In the next video, we're going to look at tuples.
Lesson notes are only available for subscribers.