Now, dictionaries can also be a little bit more complex. For instance, we can have a
nested dictionaries.
So imagine we have a family
dictionary.
So a family would have a mom
and then also you would have a dad.
So in this case here, for mom, we can have of course
a key which is mom
and then the dictionary
here also
that would represent a key,
right? So inside of this dictionary, inside of dictionary,
we can say for instance, name
Gina
like this
and let's say age 78 like that.
And for that, we are going to also create yet another dictionary
that we have. Let's make sure that this is like that
the same thing we're going to have name
age 89
and we need to put a comma there.
So now we can see we have a little bit more complex dictionary.
So we have a dictionary within a dictionary. So we have the main dictionary family
and inside we have a key, his mom, which indeed has a dictionary itself.
So in this case, mom has a name
Gina and age 78, dad has a name
Maba, age 89.
So let's see how to loop through to get certain items. I'm going to use the for loop.
Now, because of the structure that we have here,
we have a dictionary inside of another dictionary.
We can extract a few things.
So I can say, for instance, for a name,
I want to get the names of each one of those person.
And also I want to get say info information
in family
dot items
like this.
So now I'm going to be able to go through. So let's first go ahead and say, print
and print the name and information just to see what will come out. Let's run this.
OK? So you can see now it's the name, it's going to say mom,
then we have our inner
dictionary and then I have dead, we also have in a dictionary like that.
This is really cool.
So you can see that we were able to extract the correct information that we want.
Now let's say want to prop in and write something that is a little bit nicer than this.
Well,
I can come here and say, for instance, say print, let's say parent
type. Then here I'm going to actually make this an F string,
right?
And I'm going to pass the name variable, which is going to be exactly the parent type.
And then I want to have a new line to make sure that we have things organized. Well,
so to add a new line, I'm going to put backslash like this and put n actually,
that has to be inside
of our string
like that. So to say, just
carry on, give me a new line before we print it. The next thing, say parent
name
and to get the parent name in this case here, I just want to get this name here.
So what I would say
and all of that information is going to be inside of the information,
right? As you saw earlier here, information this dictionary here.
So we're going to probe in and get that
information because I know it's just a dictionary.
So I'm going to go and access name,
which is going to be in this case, Gina and next Mapa.
So I'm going to go
like this
and pass in the name as such.
Let me get rid of this and let's save and
see what is going to print actually print parent name.
Let's see.
OK, so you can see
it goes parent type is mom, her name is Gina. Parent type is dad. His name is Mapa.
Alright, so a little bit of issues with spacing and everything, but no worries.
At least we see that we're getting the right information.
We'll fix that.
OK. Let's remove that new line there to make it even better.
So OK, so we have parent name
and what I'm going to do, I'm also going to put inside of F string all of this here.
So I can get the correct
formatting and correct information like this.
So parent name,
I'm just going to go ahead and say name,
give her that
I'm going to copy all of that command, shift
arrow and then option shift
arrow down
to copy the same thing.
Let's get
the
age
for each parent. So go ahead and change the age.
So once we have extracted the information that we wanted,
now we can go ahead and display it. So before anything is printed, I'm going to
escape
a new line or create a new line like this for parent type
and then start putting everything together.
And now here I'm going to print,
put an F string backslash N for a new line and let's pass parent
name. And then at the bottom here, I'm going to use what we have already.
This is going to be
F string again and we are going to add a new line before we print this
and we're going to pass the parent
age like this.
So we are essentially passing these variables to show everything nicely. OK?
Let's just run this and see
what we're going to be able to see.
OK. So you can see here, let me put this perhaps
to the right.
OK. So we can actually see this better,
right? So you can see we have here, parent type is mom
name is Gina. Gina. So I think I am repeating everything here. Let's just make sure
and this has to be parent
age.
Alright, let's go ahead and run this real quick. So we can see.
Ok, so now it looks real good. Parent type is mom
name Gina age 78
and then we have parent type
is dad. His name is Maba, age 89.
Alright.
Ok. So you can see that we were able to look through
a dictionary that's a little bit more complicated,
a dictionary within a dictionary.
And we pull all of the information that we want.
Now, this may seem a little bit complicated first if it is your first time.
But just remember all we're doing here,
we are separating all the pieces that we want to get.
So we want to get name
and information.
So information as you said before is going
to just have the information for each individual
dictionary for each one of these
mom and dad, right?
And then we just separate the parent name, we remove that
and the age
and then we put it all together to print that way. We have something
like this.