Welcome back.
We can have multiple levels of nesting of both dictionaries and lists.
Here is a pretty complicated data structure.
Our task is going to be to extract the value associated with color.
So that's here down on line six,
the color as the value which is a dictionary,
eye blue and hair brown.
So we can use the same incremental approach that I showed for
extracting something from a heavily nested dictionary.
We're going to need one square bracket for each level that we're descending.
So we're going to need one square bracket to get the stuff associated with
personal data, and then within that we're going to need another square
bracket To get the stuff associated with physical features, and
within that we're going to need another square bracket to just get the color.
So let's do this one step at a time.
And we're supposed to make a variable called color,
whose value is going to be that dictionary, eye blue and hair brown.
We're not going to get that in one step, we're going to extract something from
the info dictionary, and let's just see how we're doing by printing it out.
And what am I going to first extract?
Well first I'm going to extract all of this,
all the stuff that is associated with personal data.
So I have to ask for the value that's associated with the key personal data.
When I run that and I ask to have it printed out, we're going to see,
oops, we got an error.
The error says I have bad token on line number 16.
So let's see if we can figure out what I did wrong.
Here's line number 16, and the color coding helps me a little bit here too.
I can see I forgot to close my string.
Let's save and run this.
And we can see that color is now bound to a smaller dictionary than before and
it's got exactly the contents, Not so
nicely printed, not so nicely formatted, but it's got exactly these contents.
And you can see that since there's a test to see whether we've finished the problem,
we have not finished the problem.
We have descended one level into this nested data structure, but
we haven't pulled out the value that we're supposed to have pulled out.
So let's go one step further.
In the dictionary that we have so far fo the value color, where does color appear?
It appears here, associated with the key physical features.
So my second level of nesting is going to ask for physical features.
And each time we descend one level, our task is going to get a little bit easier.
The reason it gets a little easier is that we have less stuff.
We've extracted more, we now have a solid data structure so
it's easy to figure out where we are.
And we want to get this dictionary,
which is associated with the key color in the thing that we have,
so let's do one more level of extraction, and we ask for color.
And now we've managed to pass the test,
because we were supposed to get the value associated with color.
Now notice that the value is still a dictionary,
we didn't get just a simple value, we still have a dictionary but
that's the thing that we were supposed to get.
Now one thing to notice about this complicated data structure,
we have all this nesting of one dictionary inside of another.
A dictionary is always a value associated with a key.
We can never have a dictionary like this be a key.
Keys of dictionaries have to be immutable objects like strings or numbers or tuples.
They can't ever be lists or dictionaries.
And one other thing to try,
suppose we wanted to change the value that was associated with a color key.
So instead of what it used to be, let's make it be something else.
Let's make it be 95.
Doesn't mean very much, but let's just try that.
Well, we can use the same trick that we did before.
Once we have an expression like info['personal_data']['color'],
We can just sign to that same expression.
because info ['personal_data']['physical_features']['c-
olor'], that picks out a location in this complicated data structure.
So I'm just going to copy this.
And before I extract it, I'm going to change it.
If I make it be the left-hand side of an assignment statement,
I can make it be 95, and now when I extract it,
I'm going to get the value 95 instead of getting eye blue and hair brown.
Of course, I've now failed the test because I haven't picked out the right
value, but I'm just trying to illustrate the idea that Info ['personal_data']
['physical_features'] ['color'], that's a way of picking out
a value deep inside of the data structure, but I can also assign.
That defines a position, and I can assign that position a new value,
95 as I've done on line 15.
So that's how you extract from end assign to complex nested lists in dictionaries.
See you next time.