1 00:00:08,648 --> 00:00:10,790 Welcome back. 2 00:00:10,790 --> 00:00:14,380 We can have multiple levels of nesting of both dictionaries and lists. 3 00:00:16,070 --> 00:00:17,707 Here is a pretty complicated data structure. 4 00:00:19,247 --> 00:00:23,860 Our task is going to be to extract the value associated with color. 5 00:00:25,780 --> 00:00:29,580 So that's here down on line six, 6 00:00:29,580 --> 00:00:34,738 the color as the value which is a dictionary, 7 00:00:34,738 --> 00:00:37,606 eye blue and hair brown. 8 00:00:37,606 --> 00:00:40,537 So we can use the same incremental approach that I showed for 9 00:00:40,537 --> 00:00:44,070 extracting something from a heavily nested dictionary. 10 00:00:44,070 --> 00:00:48,557 We're going to need one square bracket for each level that we're descending. 11 00:00:48,557 --> 00:00:52,983 So we're going to need one square bracket to get the stuff associated with 12 00:00:52,983 --> 00:00:57,337 personal data, and then within that we're going to need another square 13 00:00:57,337 --> 00:01:01,395 bracket To get the stuff associated with physical features, and 14 00:01:01,395 --> 00:01:06,143 within that we're going to need another square bracket to just get the color. 15 00:01:08,147 --> 00:01:11,380 So let's do this one step at a time. 16 00:01:14,257 --> 00:01:17,569 And we're supposed to make a variable called color, 17 00:01:17,569 --> 00:01:22,630 whose value is going to be that dictionary, eye blue and hair brown. 18 00:01:22,630 --> 00:01:27,590 We're not going to get that in one step, we're going to extract something from 19 00:01:27,590 --> 00:01:32,710 the info dictionary, and let's just see how we're doing by printing it out. 20 00:01:35,010 --> 00:01:37,550 And what am I going to first extract? 21 00:01:37,550 --> 00:01:42,920 Well first I'm going to extract all of this, 22 00:01:44,660 --> 00:01:47,510 all the stuff that is associated with personal data. 23 00:01:48,750 --> 00:01:54,443 So I have to ask for the value that's associated with the key personal data. 24 00:01:56,990 --> 00:02:01,680 When I run that and I ask to have it printed out, we're going to see, 25 00:02:01,680 --> 00:02:03,400 oops, we got an error. 26 00:02:05,220 --> 00:02:09,558 The error says I have bad token on line number 16. 27 00:02:09,558 --> 00:02:11,530 So let's see if we can figure out what I did wrong. 28 00:02:12,650 --> 00:02:17,490 Here's line number 16, and the color coding helps me a little bit here too. 29 00:02:17,490 --> 00:02:20,537 I can see I forgot to close my string. 30 00:02:22,414 --> 00:02:24,890 Let's save and run this. 31 00:02:28,448 --> 00:02:35,419 And we can see that color is now bound to a smaller dictionary than before and 32 00:02:35,419 --> 00:02:41,339 it's got exactly the contents, Not so 33 00:02:41,339 --> 00:02:46,642 nicely printed, not so nicely formatted, but it's got exactly these contents. 34 00:02:49,137 --> 00:02:52,881 And you can see that since there's a test to see whether we've finished the problem, 35 00:02:52,881 --> 00:02:55,190 we have not finished the problem. 36 00:02:55,190 --> 00:02:58,880 We have descended one level into this nested data structure, but 37 00:02:58,880 --> 00:03:02,070 we haven't pulled out the value that we're supposed to have pulled out. 38 00:03:03,850 --> 00:03:06,970 So let's go one step further. 39 00:03:06,970 --> 00:03:13,508 In the dictionary that we have so far fo the value color, where does color appear? 40 00:03:15,323 --> 00:03:21,610 It appears here, associated with the key physical features. 41 00:03:21,610 --> 00:03:29,000 So my second level of nesting is going to ask for physical features. 42 00:03:30,970 --> 00:03:35,813 And each time we descend one level, our task is going to get a little bit easier. 43 00:03:38,978 --> 00:03:42,550 The reason it gets a little easier is that we have less stuff. 44 00:03:42,550 --> 00:03:45,524 We've extracted more, we now have a solid data structure so 45 00:03:45,524 --> 00:03:47,401 it's easy to figure out where we are. 46 00:03:50,534 --> 00:03:53,302 And we want to get this dictionary, 47 00:03:53,302 --> 00:03:58,456 which is associated with the key color in the thing that we have, 48 00:03:58,456 --> 00:04:03,631 so let's do one more level of extraction, and we ask for color. 49 00:04:07,871 --> 00:04:10,285 And now we've managed to pass the test, 50 00:04:10,285 --> 00:04:14,197 because we were supposed to get the value associated with color. 51 00:04:14,197 --> 00:04:17,905 Now notice that the value is still a dictionary, 52 00:04:17,905 --> 00:04:22,879 we didn't get just a simple value, we still have a dictionary but 53 00:04:22,879 --> 00:04:26,506 that's the thing that we were supposed to get. 54 00:04:26,506 --> 00:04:29,986 Now one thing to notice about this complicated data structure, 55 00:04:29,986 --> 00:04:33,346 we have all this nesting of one dictionary inside of another. 56 00:04:35,078 --> 00:04:40,640 A dictionary is always a value associated with a key. 57 00:04:40,640 --> 00:04:45,729 We can never have a dictionary like this be a key. 58 00:04:47,503 --> 00:04:52,780 Keys of dictionaries have to be immutable objects like strings or numbers or tuples. 59 00:04:52,780 --> 00:04:55,176 They can't ever be lists or dictionaries. 60 00:04:58,523 --> 00:05:00,345 And one other thing to try, 61 00:05:00,345 --> 00:05:05,348 suppose we wanted to change the value that was associated with a color key. 62 00:05:08,559 --> 00:05:15,755 So instead of what it used to be, let's make it be something else. 63 00:05:15,755 --> 00:05:17,982 Let's make it be 95. 64 00:05:17,982 --> 00:05:22,683 Doesn't mean very much, but let's just try that. 65 00:05:24,109 --> 00:05:27,370 Well, we can use the same trick that we did before. 66 00:05:28,520 --> 00:05:34,633 Once we have an expression like info['personal_data']['color'], 67 00:05:36,302 --> 00:05:39,848 We can just sign to that same expression. 68 00:05:39,848 --> 00:05:45,050 because info ['personal_data']['physical_features']['c- 69 00:05:45,050 --> 00:05:49,310 olor'], that picks out a location in this complicated data structure. 70 00:05:50,430 --> 00:05:51,813 So I'm just going to copy this. 71 00:05:56,719 --> 00:05:59,650 And before I extract it, I'm going to change it. 72 00:06:01,170 --> 00:06:05,389 If I make it be the left-hand side of an assignment statement, 73 00:06:05,389 --> 00:06:08,571 I can make it be 95, and now when I extract it, 74 00:06:08,571 --> 00:06:13,451 I'm going to get the value 95 instead of getting eye blue and hair brown. 75 00:06:15,945 --> 00:06:20,939 Of course, I've now failed the test because I haven't picked out the right 76 00:06:20,939 --> 00:06:26,637 value, but I'm just trying to illustrate the idea that Info ['personal_data'] 77 00:06:26,637 --> 00:06:31,635 ['physical_features'] ['color'], that's a way of picking out 78 00:06:31,635 --> 00:06:35,950 a value deep inside of the data structure, but I can also assign. 79 00:06:37,370 --> 00:06:41,920 That defines a position, and I can assign that position a new value, 80 00:06:41,920 --> 00:06:44,049 95 as I've done on line 15. 81 00:06:45,748 --> 00:06:51,960 So that's how you extract from end assign to complex nested lists in dictionaries. 82 00:06:51,960 --> 00:06:52,780 See you next time.