In this session, we will discuss about dictionaries dictionary is an unordered collection of key value
pairs with unique piece.
These are unique within a dictionary, while the values may defeat.
The values of a dictionary can be of any data, while the keys must be awful, immutable data such as
string numbers or doubles.
Let us see how we can create a dictionary.
Let us start by creating a dictionary so we can create a dictionary.
By simply giving you the name, say.
They one.
And to create a dictionary, we need the holy places inside the holy places, we will provide the key
value pairs separated by a comma.
So we will give the first key, which could occur key can have values like strength numbers or double.
So let us play the string.
Same name.
And the value.
Will be, let us say, Munsie.
We separated, we see each.
And the age is say.
Twenty seven VCM.
Plus.
That is given the number of this, say, 12.
And the value can be anything.
So let us give it a value as a list.
Now we can have Cappadocia, so let us.
Played, you sort of have any values, say, one, two, three, and the value will be one.
So here we have.
A key value pair with a key is name a string, the device and the value is, again, a string device.
The next one is a piece with string and value as no key, and we have key as no value as a list here.
We have key as a couple and value as a number.
Let me run this and let us see the dictionary.
So here you can see we are able to create this dictionary.
Now, if you try to create, you can have values as boolean values.
So here I have just created a boolean value.
Now let's try to have a key value pin, which is maybe a list.
Azadeh, so let us read this and let the value of one.
Now, when we run this, you can see the it gives a bipod error, that is the type is unmatchable because
Alist cannot be a part of the key.
So we can simply remove this now.
Let us create another dictionary.
And we will try different operations on the Pacific.
So let's create a dictionary of.
A person.
C student.
And.
Let us create the dictionary.
With.
Name.
As John.
Ange.
14.
Then we can have.
Other details.
Like.
School.
And this is say.
ABC.
So this is the dictionary which we have created and we can print it using STOO.
Now if I want to print a particular value from it.
So remember that a dictionary is unordered in nature.
So if I try to pull something like.
One so.
It will not doing anything because this is not a valid key, what did XM expect?
Here is a key, a key that you have created.
You will have to input that here.
So we will put let's same school.
So if you run this, you will get the value of the school.
Similarly.
If you run this with.
Aneesh, we will get the value of each.
Now, let's say we want to retrieve all the elements from this particular.
Dictionary, then what we can do is we can send see student items and when we done this, we get the
entire value set present in this particular dictionary.
So it returns a list.
All couples, these are the couples, one couple containing key value, next.
The bill, again, contains a key on its value.
Now, let us say we want to retrieve a particular.
Key out of this, so what we can do is for this weekend, statically see.
S.T. Hill, he's.
And it will give me the list of the Indiagames similarly, I can see CD on values and it will give me
the list of all the values which are present now as we have these.
And let us say we want to create a new key value pair, then we can simply cease to and inside this
we can create a new pieszecki.
And I can assign the value to it, see value.
So now, if I didn't do so, you can see a new P-value beard has been added with.
You can see once we have done this, so I knew he was Lupin has been added that he is taken from here
and this is the value which we have just assigned.
Now, let us see how we can delete a particular key value fish.
So let me, first of all, bring the student for you so.
Savi.
And we can get value by using.
He.
So this is the key value pair now, if I want to believe this, I can simply say delete and it will
delete this particular key.
Now, if I look up the dictionary.
Here you can see the value is not available now.
And in case I want to remove the entire dictionary, then I can simply say dill stew and if they leave
the dictionary, so now, if you will, to try to access the dictionary.
It will give an order that is the means do is not defined.
Now let us recreate the dictionary.
So let us recreate this dictionary now.
So this is the collegiate dictionary, which we have the now let's say we want to read on top of this
dictionary, then what we can do is we can simply say for.
Gyi and.
The one that is the dictionary.
Trent.
So this gives me different values.
Now let me try to get the key and the items so that I can do simply by.
So this gives me the key and relevant.
Now let us say we want to trade on top of both key in value and we don't really want to use this particular
structure because we might not really know which dictionary we are working on.
So we can simply use the same thing.
But the difference will be now we will try to get more value here.
So now we will be trading on the dictionary items so that items.
Now, what does dictionary those items give us?
So let us see what the one item will give us.
So this gives us the Thumper's that is the key and the corresponding value on the key, on its corresponding
value and so on.
So then we will be getting the values.
This will be.
In fact, so we will get key karma value from it, so instead of using this, we can simply say value
and this will bring the key and value the.
So this is how we can access elements from a dictionary and use it in a for loop, so always remember,
if you want to access a particular dictionary, then we will be using keys for accessing the dictionary.
So whenever we will be looping on a dictionary, it will loop on the keys.
So we will get keys here and then we can decode corresponding key and values.
Now is we are moving on the items.
This means that we are looping on this particular list of papers which contains key and value mode.
So it will be unpacked and we get key and value in this particular order, only that the values will
always be the first one which we will be getting would be key.
And the second one which we will be getting is the value.
So this is about dictionaries.
Thank you.