- [Instructor] Yet another data type is a dictionary.
And similar to a physical dictionary
where multiple words are associated
with their own definitions,
a Python dictionary can have multiple keys,
and each can have its own value.
And this is an unordered set of key value combinations.
That's different than the list data type
we saw in a previous video.
With a list data type, I could say,
"Show me the first item in the list."
But with a dictionary, since each key has its own value,
we can say, "Show me the value for a specific key."
It doesn't matter where it is in the dictionary.
Let's create a dictionary named ipaddress.
And we'll say that this dictionary
is going to contain the IP addresses
for different network devices,
different routers and switches.
Remember that when we created a list,
we gave the list name, we said equals,
and then we put the list contents in straight brackets.
Well, with a dictionary, we use curly brackets.
So I'm saying, ipaddress equals
and then open curly brackets.
And I want to say that the IP address
for router A is 10.10.10.1.
I'll give the key of router A.
I'll say in quotes because it's a string.
I'll say, RTR hyphen A close quote.
And the separator between a key and its value is a colon.
So the key is RTR hyphen A.
And the separator is a colon,
and the value of RTR-A, its IP address in this case,
is, and this is also a string, so I put it in quotes,
it's 10.10.10.1.
Let's do a close quote.
And that's my first key value pair.
But we can have multiple key value pairs in the dictionary.
We separate key value pairs with a comma.
So I'll give a comma
and I'll say in quotes, RTR hyphen B, close quote,
give my colon separator
and give a value of 10.10.20.1 close quote.
Let's give another comma and another key value pair.
Let's do a switch now.
I'll say in quotes SW hyphen A, close quote, colon separator
and the value for switch A
is going to be 10.10.30.1 close quote.
Let's do one more.
I'll give a comma separator
and we'll say for switch B,
we've got a value of 10.10.40.1,
close quote, close curly brackets,
and we press enter.
we've now created our dictionary.
And we can verify
that IP address is in fact a dictionary data type
by using the type function.
I'll say type, and then in parentheses,
I'll say ipaddress.
And it says that the class,
in other words, the data type is a dictionary.
Remember, with a list we could look up the value
for a specific position in the list.
However, with a dictionary,
our query might ask for the value associated with a key.
If I wanted to see the value associate with switch A,
I want to see its IP address, I can say,
ipaddress and then in straight brackets,
when I'm doing a query, I'll say,
in quotes SW hyphen A close quote, close straight brackets.
And it says the value of switch A is 10.10.30.1.
If I want to see my entire dictionary,
I can just put in the variable name of ipaddress,
and that shows everything.
I could add a value to my dictionary,
this way I could say, ipaddress,
and let's say that I wanted to add a key of router C.
So in straight brackets, notice I'm not saying equals,
I'm not trying to recreate the variable.
I'm just adding on to it.
So there's no equals sign and I'm using straight brackets.
I'll say, open straight brackets,
and then I'll give the key in quotes
because it's a string of RTR hyphen C, close quote,
close straight brackets.
I'm saying, I want to assign a value to this key.
And I say that that equals, in quotes,
because it's a string, it equals the IP address
of 10.10.50.1 close quote,
and we press Enter.
Now, when we say,
"Show me the entire IP address dictionary,"
you see that router C has been added.
And if I want to verify
that a particular key is present in a dictionary,
without necessarily saying, "Show me the value,
"I just want to see if the key of router C is there,"
I can say, in quotes, RTR hyphen C close quote.
And I use the keyword of in.
Remember, we use that in our Boolean evaluations.
Is it in the variable, which is a dictionary, of ipaddress?
It is.
So the Boolean return is a true.
And that is a look at the data type of a dictionary.