1 00:00:07,940 --> 00:00:13,080 Welcome back. In the past we've stuck in a few nested lists, 2 00:00:13,080 --> 00:00:17,520 but without ever really looking closely at what they are or how to work with them. 3 00:00:17,520 --> 00:00:21,855 Here we have a list called nested1, 4 00:00:21,855 --> 00:00:25,840 who's items are themselves lists. 5 00:00:30,860 --> 00:00:34,320 So, here's the first item a, b, c, 6 00:00:34,320 --> 00:00:36,120 a second item d e, 7 00:00:36,120 --> 00:00:39,240 and the third item is a list f, g, 8 00:00:39,240 --> 00:00:46,595 h. I'll talk about the whole list nested1 as the outer list, 9 00:00:46,595 --> 00:00:52,810 and each of these smaller lists inside as the inner lists. 10 00:00:52,810 --> 00:00:56,750 So, the first item in the outer list is an inner list a, b, 11 00:00:56,750 --> 00:01:03,320 c. I'm going to comment out some of the later lines of code here, 12 00:01:03,320 --> 00:01:05,390 and we'll work our way through it, 13 00:01:05,390 --> 00:01:09,640 seeing what the effect is of just one line at a time. 14 00:01:09,640 --> 00:01:14,245 On line two, we're printing the first item from the outer list, 15 00:01:14,245 --> 00:01:21,840 which is the first inner list a, 16 00:01:21,840 --> 00:01:29,710 b, c. Now I'm going to uncomment line three and run it. 17 00:01:29,710 --> 00:01:33,190 We're going to ask for the length of nested1. 18 00:01:33,190 --> 00:01:36,300 The length of that is three items. 19 00:01:36,300 --> 00:01:37,980 It's not a, b and c, 20 00:01:37,980 --> 00:01:42,015 it's item one, item two, and item three. 21 00:01:42,015 --> 00:01:45,430 So three items in nested1. 22 00:01:48,740 --> 00:01:54,540 I can append a new item as we've seen before, 23 00:01:54,540 --> 00:01:59,830 and nested1 is now going to have four items. 24 00:01:59,830 --> 00:02:03,770 The last item will be a list containing just i, 25 00:02:04,080 --> 00:02:07,255 and I can iterate through these lists, 26 00:02:07,255 --> 00:02:09,165 and print each of them out. 27 00:02:09,165 --> 00:02:13,010 So, this is going to print out all four of the lists. 28 00:02:14,480 --> 00:02:18,360 We get the first inner list, 29 00:02:18,360 --> 00:02:21,720 the second one, and the third one, and the fourth one. 30 00:02:21,720 --> 00:02:25,765 So, it's helpful to see the code lens representation of a nested list. 31 00:02:25,765 --> 00:02:28,340 Let's see this in code lens. 32 00:02:28,340 --> 00:02:34,870 So, when I assign this variable nested1 to have a nested list as its value, 33 00:02:34,870 --> 00:02:38,400 you can see it's printed representation. 34 00:02:38,400 --> 00:02:44,660 And it gets a little funny here because code lens is sometimes deciding to make 35 00:02:44,660 --> 00:02:52,490 an inner list be just shown right inside the outer list, 36 00:02:52,490 --> 00:02:56,480 and sometimes it's deciding that it doesn't really have room to put a, b, 37 00:02:56,480 --> 00:03:03,330 c here, so it's showing us the pointer to the inner list. 38 00:03:03,330 --> 00:03:07,800 So, nested1 again is a list of three items. 39 00:03:07,870 --> 00:03:12,285 The first item is this inner list, 40 00:03:12,285 --> 00:03:15,490 the second item is d e, 41 00:03:15,490 --> 00:03:18,560 and the third item is the inner list f, 42 00:03:18,560 --> 00:03:26,915 g and h. Let's see what happens if I append an item to each of the inner lists. 43 00:03:26,915 --> 00:03:33,335 So, nested1 square bracket zero, 44 00:03:33,335 --> 00:03:37,550 that's referring to the first inner list, 45 00:03:37,550 --> 00:03:42,440 and I'm going to append a new item to that, making it a, b, 46 00:03:42,440 --> 00:03:48,660 c and z. I'm also going to 47 00:03:48,660 --> 00:03:56,025 append something to the second inner list. 48 00:03:56,025 --> 00:03:59,740 Let's see what happens in code lens again. 49 00:03:59,740 --> 00:04:06,340 So, we get this original view of nested1 Of what the reference diagram looks like. 50 00:04:06,340 --> 00:04:12,620 Now what happens to that first inner list when I append Z to it? 51 00:04:13,100 --> 00:04:16,650 Well you'll see this odd thing has happened. 52 00:04:16,650 --> 00:04:22,535 Instead of having this pointer out to the other inner list, 53 00:04:22,535 --> 00:04:26,360 it's now magically decided that it has enough room to put it right here. 54 00:04:26,360 --> 00:04:28,030 So, this doesn't actually make any difference, 55 00:04:28,030 --> 00:04:31,135 it's just how code lens is choosing to show it to us, 56 00:04:31,135 --> 00:04:35,450 and it's put a fourth item in there. 57 00:04:35,840 --> 00:04:43,770 If we now see what happens when we append w to the second inner list, 58 00:04:43,770 --> 00:04:52,400 we find that it's now put that here and instead of keeping the list right in here, 59 00:04:52,400 --> 00:04:54,525 it's made a pointer out to here. 60 00:04:54,525 --> 00:04:57,800 So either way it's a way of representing that 61 00:04:57,800 --> 00:05:01,865 this item in the outer list is itself a list. 62 00:05:01,865 --> 00:05:05,270 We can put all the list items right in there, 63 00:05:05,270 --> 00:05:08,960 or we can make a pointer out to another inner list. 64 00:05:08,960 --> 00:05:11,600 So, these are two alternative ways that you can 65 00:05:11,600 --> 00:05:16,070 represent a nested list in these reference diagrams. 66 00:05:16,070 --> 00:05:26,035 You can do double indexing with two square brackets to pull an item out of an inner list. 67 00:05:26,035 --> 00:05:30,380 So, the first indexing we're going to do on line 68 00:05:30,380 --> 00:05:34,520 two is we say nested one square brackets one, 69 00:05:34,520 --> 00:05:39,330 that pulls out one of the inner lists, 70 00:05:39,330 --> 00:05:46,125 and y is now bound to that list containing 71 00:05:46,125 --> 00:05:55,110 d and e. On line three when I print y, 72 00:05:55,110 --> 00:06:04,480 I should just get the list d e as my output. 73 00:06:05,060 --> 00:06:11,050 On line four, if I ask for y square brackets zero, 74 00:06:11,050 --> 00:06:14,480 that should get me the first item in y. 75 00:06:14,480 --> 00:06:18,670 So, I should get d printing out. 76 00:06:22,460 --> 00:06:26,375 Now let's think about what happens on line six. 77 00:06:26,375 --> 00:06:36,330 On line six, I also have two square brackets but it's not delving into a big nested list. 78 00:06:36,330 --> 00:06:40,845 The first square brackets is actually used to define a list, 79 00:06:40,845 --> 00:06:43,330 to create a list. 80 00:06:44,240 --> 00:06:48,590 So, we create a list 10, 20 and 30, 81 00:06:48,590 --> 00:06:52,945 and then we pull out the item at position one, 82 00:06:52,945 --> 00:06:57,530 so what we should get as our output is 20. 83 00:06:57,560 --> 00:07:02,470 On line number seven we also have to square brackets, 84 00:07:02,470 --> 00:07:07,105 but in this case, before either of the square brackets we have nested1, 85 00:07:07,105 --> 00:07:10,140 which refers to this big list, 86 00:07:10,140 --> 00:07:13,415 the outer list that has inner lists as elements, 87 00:07:13,415 --> 00:07:18,005 and we are first grabbing the thing at square brackets one, 88 00:07:18,005 --> 00:07:20,925 and then we are from that, 89 00:07:20,925 --> 00:07:23,700 we are grabbing the thing at square bracket zero. 90 00:07:23,700 --> 00:07:30,190 So this is going to print D. How does Python know, 91 00:07:30,190 --> 00:07:35,165 in a situation like this when there are two square brackets, one after the other. 92 00:07:35,165 --> 00:07:41,195 Is it indexing twice to get inside an outer list and then inside an inner list, 93 00:07:41,195 --> 00:07:43,820 or is it as in line six, 94 00:07:43,820 --> 00:07:48,730 is it creating a list and then indexing into that list? 95 00:07:48,730 --> 00:07:50,150 And the answer is that, 96 00:07:50,150 --> 00:07:53,075 the Python interpreter can tell just by the context. 97 00:07:53,075 --> 00:07:56,495 If there's a list before the square bracket, 98 00:07:56,495 --> 00:08:00,070 then the square bracket means index go into that list. 99 00:08:00,070 --> 00:08:03,080 If there's no square bracket before it as with 10, 20, 100 00:08:03,080 --> 00:08:04,825 30 on line six, 101 00:08:04,825 --> 00:08:08,080 then it means create a list. 102 00:08:08,080 --> 00:08:11,660 You already saw previously that we could append to 103 00:08:11,660 --> 00:08:15,095 a list even if it's a complicated list, 104 00:08:15,095 --> 00:08:22,555 but you can also assign values to positions in both the outer list and in inner lists. 105 00:08:22,555 --> 00:08:26,825 The way you do that is you make an expression that picks out a list position, 106 00:08:26,825 --> 00:08:31,610 like nested square bracket one, 107 00:08:31,610 --> 00:08:34,955 but you put it on the left side of an equals sign. 108 00:08:34,955 --> 00:08:38,505 On the right side you say what new value you want. 109 00:08:38,505 --> 00:08:42,050 So, line number two here is going to say 110 00:08:42,050 --> 00:08:47,165 nested square bracket one refers to the position where currently we have d and e, 111 00:08:47,165 --> 00:08:53,210 and it's going to replace that d and e with the list 1,2,3. 112 00:08:53,210 --> 00:08:57,200 And we can take that same logic one step further on line three. 113 00:08:57,200 --> 00:09:08,165 Nested square bracket one square bracket zero goes to the list that's at position one, 114 00:09:08,165 --> 00:09:15,605 and it finds within that inner list the item that's at position zero, 115 00:09:15,605 --> 00:09:20,265 and it replaces that item with the value 100. 116 00:09:20,265 --> 00:09:25,110 So, let's see what happens when we run this in code lens. 117 00:09:27,850 --> 00:09:33,090 But first we create this nested list. 118 00:09:33,090 --> 00:09:34,875 You can see there are four items. 119 00:09:34,875 --> 00:09:37,070 The first item is the inner list a, b, c, 120 00:09:37,070 --> 00:09:41,550 the second item is the inner list d e and so on. 121 00:09:42,200 --> 00:09:47,650 On line two when I replace nested one square bracket one with a new value, 122 00:09:47,650 --> 00:09:50,090 let's see what's going to happen. 123 00:09:52,740 --> 00:09:57,445 Notice what happens with the second item, 124 00:09:57,445 --> 00:10:00,830 it's now the list one, two, three. 125 00:10:00,830 --> 00:10:03,965 It used to be a list DE. 126 00:10:03,965 --> 00:10:07,765 Let's just back up in our execution and we'll see that. 127 00:10:07,765 --> 00:10:16,945 So, it used to be DE but when we move forward, 128 00:10:16,945 --> 00:10:23,150 that DE got replaced by the one, two, three. 129 00:10:25,140 --> 00:10:31,945 On line three, we're taking nested square bracket one square bracket zero. 130 00:10:31,945 --> 00:10:35,905 So, nested is this list. 131 00:10:35,905 --> 00:10:44,830 Position one is this list and position zero within that is right there, 132 00:10:44,830 --> 00:10:46,870 currently containing the value one. 133 00:10:46,870 --> 00:10:50,870 We're going to make that one be 100. 134 00:10:52,890 --> 00:11:00,790 Let's see that, see if that one becomes a 100 and sure enough it did. 135 00:11:03,120 --> 00:11:07,075 We can have other complex objects as list elements. 136 00:11:07,075 --> 00:11:11,090 For example, here each of the items is a dictionary. 137 00:11:11,130 --> 00:11:16,670 Let's see how Code Lens chooses to show that. 138 00:11:17,550 --> 00:11:22,270 So, we have a variable nested two. 139 00:11:22,270 --> 00:11:24,970 Its value is a list. 140 00:11:24,970 --> 00:11:31,795 That list has three items and each of those items is a dictionary. 141 00:11:31,795 --> 00:11:37,825 So, the first item nested inside of our outer list is a dictionary. 142 00:11:37,825 --> 00:11:38,980 It has two keys, 143 00:11:38,980 --> 00:11:41,815 a and b and each have values one and three. 144 00:11:41,815 --> 00:11:45,100 The second item in the list is also a dictionary. 145 00:11:45,100 --> 00:11:48,530 It has keys a, c and five. 146 00:11:49,320 --> 00:11:53,480 Here we have an even more nested list. 147 00:11:54,300 --> 00:11:58,885 Our task is to pull out the word willow, 148 00:11:58,885 --> 00:12:02,795 which is nested pretty far in here. 149 00:12:02,795 --> 00:12:05,940 It's not at the top level of the list, 150 00:12:05,940 --> 00:12:08,655 it's not even at the second level. 151 00:12:08,655 --> 00:12:14,035 You can see that here's one of the list items, 152 00:12:14,035 --> 00:12:18,089 and it has one of its values, 153 00:12:18,089 --> 00:12:20,055 the list of willow merchant Elm. 154 00:12:20,055 --> 00:12:23,070 Then willow is a list inside of that. 155 00:12:23,070 --> 00:12:25,895 We actually have one square bracket here, 156 00:12:25,895 --> 00:12:28,480 second square bracket, a third square brackets. 157 00:12:28,480 --> 00:12:34,015 So, we're three levels deep inside of the data structure, 158 00:12:34,015 --> 00:12:36,190 inside the variable data. 159 00:12:36,190 --> 00:12:39,220 So, if it's three levels in, 160 00:12:39,220 --> 00:12:44,125 we're actually going to need three square brackets total, 161 00:12:44,125 --> 00:12:49,070 to pull out the word willow from inside of data. 162 00:12:49,290 --> 00:12:53,935 Now, I'd like to develop my extraction expressions incrementally. 163 00:12:53,935 --> 00:12:57,865 That is, I'm going to do it a little bit at a time. 164 00:12:57,865 --> 00:13:03,700 I'm going to first make the expression that pulls out something from within that. 165 00:13:03,700 --> 00:13:04,930 I'm going to pull out a little more and 166 00:13:04,930 --> 00:13:07,630 then within that I'm going to pull out a little more. 167 00:13:07,630 --> 00:13:10,315 So, let's do that incrementally. 168 00:13:10,315 --> 00:13:14,515 You can see sort of my approach to writing code for something like this. 169 00:13:14,515 --> 00:13:18,625 Now they told us to name our variable plant. 170 00:13:18,625 --> 00:13:24,385 I'm going to say plant is data square brackets something. 171 00:13:24,385 --> 00:13:29,050 I'm going to just print plant to help myself see what I got. 172 00:13:29,050 --> 00:13:32,125 Now, what item am I going to get? 173 00:13:32,125 --> 00:13:33,490 Where am I going to index? 174 00:13:33,490 --> 00:13:36,009 Well, this is zero, 175 00:13:36,009 --> 00:13:42,400 one, index two, index three, four. 176 00:13:42,400 --> 00:13:45,025 Bacon is five. 177 00:13:45,025 --> 00:13:49,150 This inner list is six and 178 00:13:49,150 --> 00:13:53,515 this inner list which contains willow somewhere in it, is seven. 179 00:13:53,515 --> 00:13:59,650 So, that tells me I should extract data square brackets seven. 180 00:13:59,650 --> 00:14:02,515 Now, that's not going to get me just the word willow. 181 00:14:02,515 --> 00:14:05,920 I could try to make a more complicated expression, 182 00:14:05,920 --> 00:14:09,850 where I fill in the others right away but I do like to do this incrementally, 183 00:14:09,850 --> 00:14:11,905 because I might have counted wrong or whatever. 184 00:14:11,905 --> 00:14:17,630 Let's just see if I got the right kind of thing from data square brackets seven. 185 00:14:17,870 --> 00:14:21,700 So, I did pick out the right element from data. 186 00:14:21,700 --> 00:14:23,395 The right inner list, 187 00:14:23,395 --> 00:14:24,940 you can see that willows in here 188 00:14:24,940 --> 00:14:29,560 somewhere but I'm going to have to do another extraction. 189 00:14:29,560 --> 00:14:33,385 Since this is an exercise that has an auto checker for us, 190 00:14:33,385 --> 00:14:35,230 it's told us that we failed. 191 00:14:35,230 --> 00:14:37,900 We were supposed to get just willow but instead we 192 00:14:37,900 --> 00:14:40,570 got this list that somewhere has willow in it. 193 00:14:40,570 --> 00:14:42,460 So, we have a little more work to do. 194 00:14:42,460 --> 00:14:45,280 We knew we were going to have more work to do. 195 00:14:45,280 --> 00:14:48,700 So, inside data square brackets seven, 196 00:14:48,700 --> 00:14:53,200 I now have some help because I can see what it looks like, 197 00:14:53,200 --> 00:14:58,730 and it's actually in the first element, square bracket zero. 198 00:15:02,070 --> 00:15:04,180 So, I'll do this and check, 199 00:15:04,180 --> 00:15:05,740 make sure I still have it right. 200 00:15:05,740 --> 00:15:10,880 Sure enough. While I was in that and which element is it? 201 00:15:11,030 --> 00:15:18,865 Position zero, one, two. I want willow. 202 00:15:18,865 --> 00:15:24,050 So, I'm going to say square bracket zero. 203 00:15:31,140 --> 00:15:34,705 Sure enough. I now have just willow, 204 00:15:34,705 --> 00:15:36,445 which is what I was looking for. 205 00:15:36,445 --> 00:15:39,560 The test tells us that we passed. 206 00:15:40,980 --> 00:15:44,770 So, when you see something like square brackets seven, 207 00:15:44,770 --> 00:15:46,600 square bracket zero, square bracket zero, 208 00:15:46,600 --> 00:15:52,690 that means you're descending several layers into a complicated data structure. 209 00:15:52,690 --> 00:15:57,700 The best way to create expressions like that is to just do it one at a time, 210 00:15:57,700 --> 00:16:02,305 and keep building it up until you've descended farther and farther into the list. 211 00:16:02,305 --> 00:16:04,105 That's nested list. 212 00:16:04,105 --> 00:16:06,850 You can index into them with repeated use of 213 00:16:06,850 --> 00:16:13,195 the square brackets or you can assign values or append deep inside the nesting. 214 00:16:13,195 --> 00:16:20,665 You can say something like data square bracket seven, 215 00:16:20,665 --> 00:16:27,040 square bracket zero.append of 216 00:16:27,040 --> 00:16:32,530 some other value or you can say data square brackets seven, 217 00:16:32,530 --> 00:16:39,830 square bracket zero and have that on the left side of an assignment statement. 218 00:16:44,670 --> 00:16:49,930 The append will assume that there is a list at data square bracket 219 00:16:49,930 --> 00:16:55,645 seven square bracket zero and it'll give that list another item at the end, 29. 220 00:16:55,645 --> 00:16:58,150 If we just do an assignment statement, 221 00:16:58,150 --> 00:16:59,770 we will replace whatever's there, 222 00:16:59,770 --> 00:17:01,330 even if it's a whole list, 223 00:17:01,330 --> 00:17:06,010 it's just going to replace it with one new value, 42. 224 00:17:06,010 --> 00:17:08,810 See you next time.