Welcome back. So if you
listen, we talked about dictionaries, but now
we're going to go a step further to talk about nested dictionaries.
So basically dictionary inside of another dictionary.
So far, we've been dealing with one single user info.
But what if we had multiple users, right? How can we create this? So
what I'm gonna do right here is let us
change the name from user info to user credentials.
All right,
this is going to be the new name for our dictionary.
And now curly braces,
we can now add the very first nested dictionary
that will contain the information for
admin.
So
gonna come right here, type in admin.
That will be our first user.
And now the credentials,
curly braces. Let's go with the password
password in here
and then
call on
and then we can go with uh admin
123.
All right.
So the first user here admin, the password is going to be admin 123
and we can add another
credential, which would be the last log in.
So last login,
let's have this one in our coats,
last log in
and now colon,
let's add the date So let's say something like 20
24-10 dash 19.
All right,
just as an example, there is a bit of an error right here. Let me see.
What exactly am I doing wrong?
Ah
Over here with password, the column should be outside.
I made a mistake there.
Ok. Much better. So this is the first and
the dictionary we have admin
that has the key value pairs, it has the key password
and the developer password here is going to be admin 123.
And then the other key here is going to be last login.
And the value is of course 2024 1019, but we can add another dictionary.
So I'm gonna say comma
and now we can add another user, let's call this person user one.
And I'm just gonna let Raley do the rest fill out
all the information in there
and there you go. So the password is going to be user 1123.
And then last login is also now let's change the date here to something else. So
let's go with 06
and then maybe
a
14, all right, just for variety.
And there it is.
So this right here is what we refer to
as a nested dictionary dictionaries inside of another dictionary.
And of course, we can access the information pretty straightforward. So
for example, if I wanted to print out the password
for the admin,
how can I do this? I can simply say print
and now in brackets, I'm going to say user
underscore credentials.
Ok. And now
the regular brackets
and inside double codes, I'm gonna say admin.
Ok.
And now we want to print out what the password for the admin. So
brackets again,
let's add
password
and there it is.
So if I run the program right now, you can see it says admin 123 because we're saying,
hey, print out from the user credentials dictionary,
we want you to print out
the value
of the key password belonging to the user admin.
And that's why we have admin 123
or I could say, OK,
print
and backet user
credentials. And let's print out the last login for user one.
How are we going to do this again? Very straightforward.
I'm simply going to come in here, say user one
and now
new brackets and then simply put in the last login key last underscore
login
and there you go. So I run
and there you go 2024
06 14.
So this right here is an example of nested dictionaries. And
this can be particularly useful if you're trying to create
functions or programs for firewalls or a
password checking tool or a tool that check user credentials, things like that,
right?
You can use nested dictionaries for those kinds of programs.
So thank you for watching and of course, I will see you in the next class