Hello.
Welcome back.
So in this lesson we are going to look at dictionaries so dictionaries the data types similar to arrays
but works with keys and values instead of indices we access to.
I remember using the index of the member in a dictionary we access a particular member using the key
of the off the dictionary.
So let's get started.
Let's create a new project went to click over here.
What I'm going to do is every time I create a new project I have to reshape my window in order to reshape
I'm going to use save us.
And then just say no 8 over here dictionary.
So then we have a new copy of this current project and then I'm just going to clean everything from
here.
We've got a new project.
We can start with.
So let's say we create a database that contains the name of the names of people and their ages.
We can come here.
Let's start by putting a simple description here.
I'll put a comment out of call this example one and say creating a dictionary Hellboy they're creating
a dictionary Dictionary.
Right.
And let's just say print because we're going to have a number of examples would be nice to have to know
which is example one example to Exeter.
So I'm just going to put a helper here print example one like this.
OK.
Now we're here.
Let's get to our dictionary.
So to create a dictionary we can just start by using a dictionary named let's say we want a dictionary
to be called ages so I can say is of course this we've created a dictionary.
Now how do we add the keys and values to the dictionary.
Very simple.
I'll come here dictionary name ages square brackets.
I want to add a key known as Tom T or M.
What's the value for Tom.
Twenty nine and then I want to add another another another row to the database you can make.
So it's key value.
Key value.
I didn't want to say key value because they go together by the represent a row.
So let's say I want to add another person to the database.
How about Dick.
So I call this ages 0 use ages Does the name of the dictionary.
And the key is stake and the value of Dick which in this case is his age is 30.
And we can add a third person to get Harry.
Come on Tom Dick and Harry.
Harry over her and we can give Harry the youngest age he's 19.
So let's say we want to print the dictionary.
Print ages.
Let's see what we get with press control us to save.
Click here to run would click here to run module and asking to see it's printed keys and values over
here.
If you see this arrangement in a way in Python this is a dictionary printed it's got a key that value
the key to value the key to value.
Tom.
Twenty nine Dick 30 Harry.
Nineteen right.
Very simple very straightforward.
So this is how to create dictionary.
So now let's use a different way of initialize in the dictionary.
I'll come down here or call this example to I'll give it a different head in OK over here different
way of initialization I'll call this different Wolf initialization and can call this print example to
like this.
So let's say we create an a database of postcodes we use a particular area in this city and then we
use the area and I suggest the key and then the postcode as the value to let's say postcodes.
That's the name of the dictionary.
And this time rather than create the dictionary and add its key values later on we can do it all at
once.
We can open it enter and then we can add the first key.
This is Shepherd's Bush which is in London.
This their first key Shepherd's Bush.
And we can give it a value.
Would do this we use call on to assign a value we can say eight nine nine too.
And in this case in our example we've seen the value represent the postcode of the key the key over
here is Shepherd's Bush.
And the value 8 9 9 2 is the postcode.
Right.
I know you get one scene and then we put a comma and then we add the next row.
And over here we can add another area.
We can see we can set those court
and we can give it a value and the value here could be for 1 1 2.
Then we add a comma we add a third area you can see Barbican
and we give it a value.
The value could be nice here.
Eight seven.
Right.
So we've called our dictionary.
Right.
We shouldn't add more to find.
Hit enter with close to Kelly braces in the same way we can print the contents of this dictionary by
just coming down here and say print the name of the dictionary is postcodes post codes like this or
press controls to save a click over here run and then run module and as you can see we've got examples
example to Shepherd's Bush 8 9 9 2 which is the value.
This is the key.
We've got a second.
The second key which is l scored 4 1 1 2 and then we've got Barbican 9 2 8 7.
So there's another way of creating a dictionary.
You can initialize it as you create it.
That's also possible.
So no now let's see a third example.
Let's see how to iterate through a dictionary.
So I'm going to bring a new head in here and I'm just put in this these headings for your revision.
I call this a sample three as we saw in earlier lessons we iterate through a list using the for loop
and for loop what we do is as soon as we use the four key word we follow it by an iterator variable
that we create.
So we can have for X for Y for C for boy for go for item for city for country.
And these are these variables are variables that we just created without assigning types to them we
can create iterator variables on the fly in the for statement and then use it to iterate through the
list over here with dictionaries because with all keys and values we need to iterate two variables one
would iterate through the Keys and the other one would iterate through the values that see what I mean.
So I'm going to come down here and say for area.
This is the first iterator coma code.
This the second iterator in postcodes does the name of our dictionary postcodes dot item items the items
like this will put us in the column and what we want to do we want to say print
the post code of percentage as this percentage D and over here we can see percentage
area code like this let's press control s and then let's run to see what we get from this is going to
see this example three.
It says the postcode of Shepherd's Bush is 8 9 9 2 The Post good of escort court is for 1 1 2.
The Post could have Barbican become 9 0 8 7.
So this is very simple.
This how with iterated through the dictionary now lets your last example.
What if we've we've already created our dictionary and we want to delete a row from the dictionary.
How do we go about that.
Let's do that now.
I'll create a header here.
I call this example number 4.
No name.
This deletion like this and we can just print.
Example for her and to delete.
We can simply say they'll use the deal and then we'll see post code and then we can just pass the key.
The key here.
Let's say we want to delete ls code from the database of post codes to see else quotes like this.
And over here we have to use square brackets.
No no more brackets with square brackets.
OK.
So down now that we've deleted our scores less print postcode less prints or a dictionary and see what
we have.
Print post.
Goods.
Control controls to save.
Click here.
Run module OK we've got a typo cause we say postcode but what dictionaries called postcodes.
We forgot the s.
So put the S over here press gone through esta safe click Koran and then click to run the module.
Now example forces ships push 8 9 9 2 Barbican 9 0 8 7.
You see there is no longer else court here.
Compare this to this.
This how we delete from the dictionary.
So this all there is for dictionaries.
And if you have any questions send me a message or leave it in the question its area and if you'll find
in this course useful please take some time off to leave a review and I shall see you in the next lesson.