Hey everybody welcome to the next video.
So in this video we're gonna be talking about a dictionary.
So a dictionary of Python is similar to a hash map.
In other languages.
Now if you're not familiar with that what this is it's a key value pair.
It's the best thing I can do is show you an example.
So if you create a variable called temp Dick dict we can do in curly quotes just make this and give
us some room here.
So what you can do with this is you can give it a key.
So in this key input I'm naming it name next my phone.
And then the value.
So in this case some of the same Brandon.
So you can see we have a key and value now to add more to a dictionary comma and then we'll do age 26.
Now you see you can add integers floats billions.
It doesn't matter.
So what this does is this allows you to have a key and then a value associated with it.
This is pretty important because in like if you create a port scanner as an example you have the port
number which this can be a number by the way this doesn't have to be a string so you can give an important
number and then some sort of text along with that such as the banner of what the port responds from.
Or let's say some sort of software tool name that you want to run.
So it might be more than just a port scanner as an example or this could be port.
And then the hosts that were available on that port so just does a quick example.
Now this is very useful for structuring data.
So in this case we have we have a name and age which is might be useful for this particular application
might not be for others.
So you can pick what data and how you format it with a dictionary.
The good thing about dictionaries it's quick to look things up and it's easy to replace remove things
add things et cetera.
So how do we reference the name or the age.
So what you can do is do our print statement.
So you do to.
So the name then block quotes you're going to give it the key.
So in this case we'll do name.
So if we go ahead and run this we see Brandon pops out.
So if we do replace us with age old we replace that with age we could see twenty six.
So it's easy to reference things when you're looking at a dictionary now.
How do you find out.
Maybe I want to know if the name brand is in the dictionary so you can do that with an if statement
so you can do if
I'm sorry if you want to look to see if a key is in the dictionary not the value will go over how to
find a value in a little bit.
But if you want to find a key so let's say if I want to know if name is a key in the dictionary.
Right.
So if name is in the dictionary you want to print found so we go ahead and run this.
We'll see.
We'll get a name because we have it.
However if we put test in there we know that test is not the key in our dictionary.
So nothing happens.
It's not printed.
We can even go with that.
Else if
the name is in tempted
name found
and found.
Perfect.
So that's how you can look for a key inside of a dictionary again.
The key is the first the first section and how you reference things.
But how do we.
Let's say add something to a dictionary.
Right.
So we want to add something.
So we'll do it and then we say what do we want to add.
So we'll do mail.
Right.
And we set this to true.
So with this a little shortcut here is if you print and then just put the value there it'll just print
the raw data to the screen so we can see currently when we start the application we have name and age
and then we add in the key male with the value of true numbers going to print that out.
So you can see here we have age 26 male true name Brandon.
So we added it.
Right.
So what if we want to remove it.
Well that's a symbol is doing pop which is a special function on a dictionary and then you provide it
some key so male.
And one thing to note is that the keys need to be unique.
You can't have duplicate keys with the same name.
You can have duplicate values but just not the same thing.
So we'll do a print before in a print after.
Right.
So you can see here this is the before we've added male name and we just now have age and name.
So that's how we can add and remove things from a dictionary.
Now what happens if we want to look through the dictionary we want to view all the results.
So there's a few ways you can go about this you can print just the keys.
You can print just the values or you can print the values and the keys or you can gain access to the
values and the keys together.
So the first example is we're going to print out just the keys just the these sections right here.
So if we do four key which is just what I'm calling in so I can reference it with the word key in temp
dictionary we're going to print key.
So if we go ahead and launch this we'll see that we're printing age and name perfect.
We printed agent name.
So now what happens if we want to print just the values.
So we'll put a little cap here.
Give us a new line and say print values just so we have some room then for value it's just what I'm
calling it in 10 dict dot values.
So what this does the DOT values it again is a special function that will prevent or that will set up
a list which we haven't gone over yet.
Just bear with me though it'll set up a list of these and then loop through each of the data.
So if we do this
Oh sorry about that.
So it looks like we try to print values instead of value.
So then we run that book.
So we've got our before and after we remove when we print the keys.
Now we have our break here which we added right there.
Now we've got 26 and brand.
So you can see though that those are our values.
Now we'll give this another break here.
Now what happens if we want to grab if we want to actually access the key and the value so we can do
that in two different ways so with the key if you see up here this loop that we did here so we can access
the values here so we can do tempted predict and then add just add key as the parameter into here similar
to how we did this with mail and age.
Key is just the value of the it's just the name of the key is a string or whatever value happens to
be.
So if we run that route quickly we can see age 26 name brand.
So we're printing it and we're accessing the data perfectly fine.
So we can do that.
However what I want to show you is probably the more common way that you're going to look through it.
So what you do is you do four key comma value.
So two variables.
So we're using the key is the first one values the second one in 10 tempting dot items.
So this will then load for each element in the dictionary.
So each element here it's going to load the key in the value separately.
So what we can do is we can do key and give it a new line value
add up format the key value like that key column Value.
Go ahead and rerun it.
So we can see key age value twenty six key name value Brennan.
So that's how you go through and work with dictionaries.
Feel free to play around with it and dictionaries is an important concept to learn in Python.
It's very good data structure it's very quick.
It's useful for a lot of things.
So play around with it get familiar with it if you have any questions feel free to jump over the Facebook
group.
However I know I haven't mentioned it yet but every single video that we've gone through for every program
that we've written I have above every video the source code to this application with a lot more comments
and explaining on how things work.
So you have it in code form as well.