[no audio]
In this video we are going to discuss about dictionary data
structure of Python, right?
Let me open my editor. So here
I am going to define a dictionary called, so dictionaries are
represented with curly braces,
right. And then let's say 'print(my_dict)', and then I am
also printing type of your given variable structure.
So you're going to see output is a dictionary, that means
whatever you define with curly braces
that is a dictionary. That's fine.
Next thing.
Let me convert that into Boolean. You know it is a empty
dictionary, right? I am converting that into Boolean.
So, you know any empty, if you convert that into Boolean it
is always 'False'. That's what we are getting here. Already
we know this rule as well, right.
Now what I am doing is, I'm going to comment this. Now I am
going to define a dictionary with some data.
So guys whenever if you want to write a dictionary with some
data, you have to write like a key and its value pair.
I mean, let's say I am going to write this as key and this as value,
[no audio]
anything,
this is a key and value is suppose
let's say, or I can take, and one more thing guys, whenever if
you are writing either key or value, if that is a string you
have to write in quotations.
Don't forget that. Now suppose
I am feeling that 1 is my key, but I want to take that as
a number, then I can write 1 directly and its value.
Let's say 2 is a key, and its value is number 2.
Yes, all are valid ones.
So, but the thing is whenever if you are define your dictionary,
it should be like a key and its value pair representation.
That means, so this is one item actually. In this item
this is a key and it is a value.
So this is a key-value pair representation, same way key and
its value, so key-value pair representation. Key and value,
key-pair representation. Key and its value, key-value pair representation.
[no audio]
Right. Fine. Now how you can access, how you
can access the value from your dictionary?
So actually values are, if you observe, this is a value,
this is a value, this is a value,
this is a value, and this is a key,
this is a key,
this is also key. So all these are keys and all these are values.
Right, so if you want to access any value from your dictionary it
is always possible with your key only.
Let me print that, 'my_dict.'
Let me do this.
"fruit", there is a key inside of your dictionary "fruit".
Now, I want to print the value for that "fruit" key.
Yes Apple you are getting Right, so you can also use one
more syntax.
That is called my dictionary dot get let's say I'm going
to take animal.
Of course, you can take anything now see the output what
you are getting is you are getting now Let me come and all
these things.
I am going to print my dictionary.
Something like dictionary called I mean key called.
Let's say three.
Do you have three key?
No dictionary.
No, but if I try to access in this way see the output it
is giving an error key error three key 3 it is not there.
Actually, that's why you're getting this error.
But instead of this syntax if I follow print might dictionary
dot get Three.
I don't know whether three is there or not but I am trying
to get in this way see the difference between previous syntax
and this syntax the syntax is giving now in giving none instead
of giving an error.
That's why this is the best one to get any time you are key.
But if you want to rise an error or the time you can go and
take this one.
Right.
So both the ways are used to access your values based on
keys. Now, let me print your dictionary first.
So guys if you want to see how many operations are and what
are the different operations are there with your dictionary.
We have a shortcut.
Let's define some dictionary.
and CDR of x These all are the operations so already we have
seen get operation right now.
Let me do one thing.
My dictionary dot clear and after this operation I am trying
to print my dictionary so before and after that operation,
I am printing see clear simply.
It is removing all key value pair representation from your
dictionary. That's it.
And that is okay.
Yeah, actually you are accessing a value based on keys right.
Now.
What I am doing is I want to assign a new key value pair
representation. I want to add a new key value pair representation
for my dictionary.
Right.
Let's say I want to take key as 3 right and its value as
something like that three now after this just observe your
dictionary. Now you're getting this one.
No.
Three key value 3.
So this is the key value pair representation and you are
adding with the help of this syntax or simply this is the
way to add a new value based on key or dictionary but while
doing in this way by mistake you are taking now I am taking
three, but I don't know whether key is there on my dictionary
or not, but I am trying to do in this way.
Not see the output first instead of number three, you're
getting 56.
The reason is while assigning a key value based on key in
this way if key already there then value will be update.
If that key is not present in a given directory, then new
key value pair representation will be created.
That's it.
See before executing 16th step.
You know, there is no key value pair called three and number
three. That's why because of 16th line you are able to add
and in 17th line.
We are seeing that new key value pair representation is added
to your dictionary.
Now.
What I am doing is I am trying to modify my existing key
value. So while doing in this way if key is not there it
will do it will give a new key value pair representation.
If already key is there the old value will be modified.
So be careful while doing while assigning a value to your
key. right fine now, let me comment all these things.
So this is your dictionary or getting now what I am doing
is print my dictionary.
Dot Keys.
Case it will give a list of keys dictionary Keys you are
getting know.
Same way you have one more operation called.
.
Values see the output what you are getting.
You're getting only values right now.
You have something called items operation.
Let me take that.
Print your dictionary dot items see the output what you are
getting you are getting a key and value as one item see that.
right, so then already we have seen.
Yeah, copy will see first copy then yeah see nothing is there.
Y equals to my dictionary dot copy.
That's it then print why?
Right and you know, what is the purpose of copyright?
Let me print your ID of Y and then idea of my dictionary
see the output.
So IDs are different means whenever if you do copy write
a new memory location will be assigned to your variable y
but directly if you assign y equals to my dictionary the
same memory address will be referenced even for y also only
we discuss this while working with your list, right?
So I assume this is a dictionary and this is my dictionary
a variable called my dictionary and someone memory address
is there and this is representing with your my dictionary
if I straightforward if I do y equals to minus.
Dictionary what will happen why is also going to reference
reference same memory address, but I don't want to do in
that I want to create a new memory location and I want to
copy this data to here and I want to assign this memory location
to Y then at that time you have to do use dot copy operation.
That's it.
Okay, so fine.
Now, let me comment all these lines once again.
Then what is the next operation?
Get is done, right?
Yeah.
We'll go with form Keys cleared and copy done get items done
Keys done.
I will go with the first.
Pop and pop item.
I will go with update update is nothing.
But if you have already some dictionary, right, let's say
you have this dictionary and I'm having one more dictionary
called new dictionary.
equals to something called for Value for of course.
You can take number four also string no problem, right?
Now what I want to do is this key value pair.
I want to add into my old dictionary.
Then you have to do your old dictionary dot update with your
new dictionary.
That's it.
So after this just try to print your dictionary and see the
result. Right new key value pair added to your old dictionary.
So update is for that purpose.
If you want to add one dictionary to other dictionary, then
you have to use update operation on your dictionaries your
old dictionary dot update then what is the data?
You want to update your voltage?
Sorry that dictionary.
Fine then.
Let me go with pop.
see Okay, so guys as of now, what is your latest dictionary
suppose, this is your latest dictionary right?
Let me come and do some where you are print statement.
Now see your latest dictionary.
Is there something like this right.
Now?
What I am doing is I am doing simply dictionary dot pop operation.
And after that I am trying to print what is my dictionary?
See the output something wrong.
Oh, sorry.
I didn't provide a key, right?
Yeah, I have to provide a key.
Let's say key called for.
See that pop is used to remove a key value pair representation
based on key.
We are giving only key it is removing your key value pair
representation from your dictionary.
There is one more thing called pop item.
Right.
Let me run this thing and see the output my dictionary dot
pop item.
And after that I am trying to print my dick, sorry.
See the output what you are getting.
Right.
It is removing some dick key value pair representation.
Actually, it will remove randomly maybe lost value somewhere
middle or something, whatever it may be right and you can
assign this item what you are removing.
So remove removed item.
And finally print what you are removing with the help of
your pop item.
You can see that.
See, this is what you are removing with the help of Pop item.
So Pop.
I do actually removes randomly.
That's it.
Okay.
Now we done for by item.
Then we have form keys and set default.
Let me go with the from keys.
Okay, so nothing.
Is there guys let me come and all those things.
Let's say a house some keys called.
I owe you right.
I am taking as a list of case list of case.
Okay.
Now I want to create a dictionary which consists of this
case right see new dictionary equals to dictionary Dot from
Keys. Just give your keys.
Now it is going to create a new dictionary with keys as a
I do You observe the output and values are known because
we as of now, I don't have any values now you can update
whatever the key value if you want to update you can update.
Let's say I want to update new dictionary with the key called.
Yay.
I want to assign letter first letter first alphabet.
Something like that.
So after that you just print and see your dictionary.
So nothing is there guys from keys are used to create a dictionary
create a dictionary with Required Keys that's it.
Right fine now, let me go with last one.
That is set to default.
Nothing.
Is there guys as of now, let me take some dictionary called
empty dictionary.
Right.
Now what I am doing is I am trying to look for some key if
that key is there.
Okay, and if whatever the value I'm having in that I will
use that if that key is not there.
See suppose.
I don't know whether keys.
There are not if key is their use that value itself.
If it is not there update with the new value.
I mean first Observer this output then you can understand.
So set default.
I am setting for number K value 45.
Sorry for key K value 45 now after this, let me create a
dictionary with.
any any Okay.
Now what I am doing is again, I am running my dictionary
dot set default if already key is there then it won't disturb
if key is not there then it will update with suppose.
A new value now see the output now it won't modify your valuable
because already fruit is their fruit key.
Is there no dictionary with a value called Apple now the
purpose of set default is if this key is already not there
if it is not there in your dictionary then only it will take
a default value.
If it is already there.
Whatever the value is there that will only it will consider
just nothing but you know, you're trying to add a new key.
You don't know whether key is there or not?
If key is there don't disturb if key is not there then set
this value for that key.
That's it.
So that is the use of set default.
Okay.
So Guys, these are the operations on your dictionaries.
The final important point is guys previously.
The dictionary is an ordered collection of dictionary.
I mean before 3.7 version now in 3.7 version dictionary is
ordered collection.
That means the way how you are defining.
Same order you will get it.
Right see suppose.
Let's say this is your dick.
Sorry.
Let me go with your command line.
Let me copy that and paste it.
So if you print your dictionary you are getting in the same
order the way how you define.
Let me go with my python 2.
so my python 2, is there somewhere python 2 And let me Define
my dictionary here.
And let me print your dictionary.
See order has been changed.
No, actually we have started starting Keys fruit, but you
are getting an output 1 so in previous versions before 3.7
Your Dictionary was unordered collection of data, but in
3.7 you are dictionaries ordered data.
Okay ordered collection of data.
You have to remember this point.
Okay guys, thank you for watching this video.