Let's talk about dictionaries and just like with lists,
you can modify, you can add remove elements in your dictionaries.
However, the core difference between your lists and dictionaries is that
unlike in your list, where you have individual elements or
values or items
in your dictionary, we're going to have or refer to as a key value pair. So
as an example, I can have a dictionary called user info
and now equals
and I'm going to now use the curly braces. OK?
We typically use the curly braces for dictionaries.
So in here,
I can have information like the username,
which would be admin.
So right here,
username is going to be the key. While admin is going to be the value of the key,
we can also have a password.
OK?
And let's say the password here is uh let me in just as an example
and we could even have another one like let's say last
login.
OK. Last login
colon
and I can have a date, let's say 20 24-10 dash 22
just as an example. All right.
So do note that whenever you're adding your key value pairs in your dictionary.
At the end of each line, you will have the comma.
But then for the very last key value pair, there is no need to add the comma at the end.
All right. So just take note of that. So
this is what we refer to as a dictionary.
All right, we have our key value pairs,
username. The value is admin we have the key password.
The value is let me in and of course,
we have the key last login and then we have the date there as the value.
So from here,
I could decide to print out the information I could come down here
and then say print.
And now if I wanted to print, let's say the user name,
I will first start by saying user underscore info.
And now the regular brackets
in here,
I would simply
say user name
and that's it.
So now if I was to run, you can see admin
has been provided. So
this is how you can print out the values
all the keys in your dictionary,
you simply state the key whose value you want to print and
the program will do the rest.
So
now just like I said, with dictionaries, you can easily add or modify
the elements in your dictionary. So for example,
if I wanted to add some new information, like let's say the email of the user,
all I would need to do right now is I can come down here
and simply say user underscore info,
ok? And now I will have the regular brackets
and now I'll simply add the key here, which is going to be email
and then very simply,
I'm just gonna say equals
and now I can add the email address right there. So let's just say admin at
lab cyber.com.
Alright? Just as an example, right?
So if I was to come down in here right now and print
the user info,
let me just remove
the key there. So we want to print out all the user information in here.
I run the program
and there it is, it's going to print out everything. So username is admin.
The password is let me in the last login is this date and then the email ID is admin
at
lab cyber.com. We could also update,
right. We could also update the
information in our dictionary.
So once again as an example,
if I wanted to update, let's say the password, right?
So I can say user underscore info.
And now in brackets,
of course, the key here is going to be password, right? So password
and now I need to provide the new password.
So let's call it sword fish,
so fish.
And
there it is. If I run my program
now, you can see that the password has
changed to SWD fish. So
just like with your list, you can
add new key value pairs to your dictionary.
You can also modify the values of existing keys.
Also just like with lists, we do have a variety of methods
that we can actually apply on to our
dictionary.
So the very first one here I want to talk about here is going to be the get method
which will retrieve the value associated with a key.
So
for example,
let me remove all this information in here. All right,
if I wanted to print the user info
or when I get the user name,
the value of the key user name,
all I need to do right here is I can say print user.info
and then dot gets.
All right. And now in brackets again,
I will now add the key which is username.
So this right here
should print out the user name. If I run
there, it is admin.
So just like with your list that we, you know, we used a pen copy and so on,
we have the exact same methods also for dictionary and get right here is one of them.
Another method is going to be the keys where you can simply print out
all the keys that we have in the dictionary. So let me remove
all the information in here. So if I run
there you go, we have all the keys which are username, password and last log in,
we also have the opposite which would be values.
So instead of keys, I'm going to change this one over here to values. And if I run.
Now we're going to have all the values listed
in the output. Now, we could also create a loop that will go through our dictionary.
And how can we do this? Well,
we can use the for loop because we have a dictionary.
Remember that the four loops work well with lists, dictionaries and so on.
So I'm gonna say four
and now I'll have to say key.
OK. So for key
and then comma value
in
and now user underscore
info
and we have to add
another method called items,
items and now brackets colon.
And there it is,
this is how you would create your loop that will run through your dictionary.
So it's gonna say four key comma value and then in
the name of the dictionary and then you're gonna
add the items method to it.
And now all we have to do from here
is to simply print out
the
information.
So I'm going to say print
and let's use the F string. So our
regular brackets first
and now the F string now
coats
curly
braces to hold and display the key
and I will have a colon.
And now the other collibra is to hold what to hold the
value.
And now if I run
there you go,
username, admin password, let me in. And of course, the last login date right there.
So you can also use loops to run through your dictionaries, your lists and so on.
So that's it for dictionaries.
Join me in the next video where we'll now take a look at what we refer to as nested
dictionaries. I will see you then.