Hello guys. Welcome to LearnVern. We saw Tuple in the last topic.
Today we will see Dictionary. About how to work with a Dictionary under Python.
First of all, What is a Dictionary?
Dictionary is a collection in Python which works on Key & Value Pair.
It is one type of ordered container according to which the values are stored under keys.
Duplicate values are not allowed on the same key.
You can write Duplicate values but the keys should be different.
Such as Aadhar Card numbers. We cannot save 2 people’s details on 1 Aadhar Card number.
Just like that, here on a key, you cannot store 2 values on the same key.
The key is very unique and it is not repetitive. Clear?
And here, the key is immutable but its value is mutable.
What are we going to do with Dictionary? Let’s see.
We will see how to Create Dictionary, how to slice with a dictionary & we will also see how to use Dictionary methods.
Without further ado, we will move to practicals which will give us a clear picture about how to use Dictionary in Python.
Let’s begin with Practicals.
First & foremost, how is a dictionary created?
Creating a Dictionary.
Suppose I took a D1 variable in which I’ll store my Dictionary.
Such as 1. I stored Apple in 1. Stored Mango in 2. Orange on 3.
What exactly is this 1, 2 & 3? These are the keys of the Dictionary.
Suppose I want to store repeated that is Duplicate values…
Such as I took 4 and input Apple again.
Here, you can make a particular character as a key too.
Suppose I stored B in A.
This is how a dictionary is created.
Now I’ll print the Dictionary, D1.
Our dictionary is printed here.
Dictionary is made up of the pairing of Key & Values.
Key is written first & the value is written after colon.
Clear?
Now you want to check the datatype. Datatype of Dictionary.
Print. Type function and D1.
And you will get Dict. here.
Dict represents Dictionary.
I hope you understood how a Dictionary is created here.
Next what we’ll do is, store multiple values in a key in the Dictionary.
Store multiple values in a key in the Dictionary.
How to do it?
We will use List here. Store List into Dictionary.
I took a variable named D2 in which I’ll make a dictionary.
I wrote Python here. Wrote Java in the 2nd.
Now I’ll make a key named Fruit inside which I’ll store the list.
Apple, Orange, Mango.
This List is stored in a Key. If you want to store Multiple values in the Dictionary in a particular key, then you can use either List or tuple.
I’ll write Storing a list into the Dictionary on a single key.
Now I’ll print the dictionary D2. It printed that list on the key.
We just saw how to store a list on a key.
Now in the same manner, we will store tuple as well.
Tuple is also a collection.
Store Tuple into Dictionary.
I’ll write D3 here where I stored Python on 1, on 2nd I took Java and on the 3rd, I made a key named language in which I stored Tuple.
In tuple… suppose I stored Java, Python, Php.
I made a key named Language & stored Tuple in it.
Print D3. See, it stored tuple here as well.
So this is how you can store tuple in a dictionary.
I’ll write a comment here Storing a tuple into a Dictionary on a single key.
So this is how you can store tuple in a dictionary on a particular key.
Moving forward to access the value of Dictionary.
Let’s see how we will do it.
We will copy paste our 1st dictionary itself to save our time.
Now I’ll print Current Dictionary which is D1.
This is my current dictionary.
Now I have to access the values.
You can access values in multiple ways.
Let’s see how.
Access the value using a key.
I’ll take the variable D1 and use our 3rd key.
Stored it in X.
Print X. And Orange is seen outside.
Meaning the value of the 3rd key is brought outside.
There is one more method by using Get Method.
Access the value using Get method.
How will we do it?
We’ll use the D1 dictionary again.
D1 . (dot) get method.
Suppose I wrote 1 here.
Okay we’ll have to take one more variable here which is Y in which we stored using Get.
We printed Y and see, we got Apple.
Clear?
You can use the Get method in this way as well.
Understood so far? Okay.
Now I want to display all the items.
Access the value using item methods.
What do we have to do now?
Suppose I took a variable Z.
There is this D1 . (dot) items method.
Print Z. All the values will come out as tuple & inside tuple, there will be a list of key & value pair combinations.
It returns a tuple into a list. The Item method is used this way.
Clear?
Next up we will see how to change values in dictionary.
Change the value.
How will we do it? There is a very simple way.
We will once again take the D1 dictionary.
Suppose I want to display Kiwi on 3rd instead of Orange.
D1, square bracket. Write down the key. Ours is 3rd.
We will write Kiwi instead of Orange here.
Print Updated Dictionary. D1.
Did you see that Kiwi is displayed on the 3rd key instead of Orange?
This is how you can store Kiwi by using a dictionary key.
Suppose I want to update it. Yes, this was one of the updates only but I want to update using the update method.
Let’s see how to do it.
Update Dictionary by using Update method.
As we already have a dictionary, I’ll print Current Dictionary here.
My current dictionary is the one with Kiwi, why?
Because we had updated the dictionary before with its key.
Now we will update using the update method.
Update Dictionary by using Update method
There’s a very simple way. D1 . (dot) update.
You will have to pass key & value both.
Suppose on the 2nd, instead of Mango, I want to store Banana.
Now I’ll print my Updated Dictionary D1.
You can see it is Banana on the second.
I hope it is clear so far.
So this is how you can change the values in the dictionary.
Next we are going to see how to remove values from dictionary.
I took a new cell. Markdown.
Remove values from Dictionary.
First of all we will be using Pop method.
Using Pop method.
First of all we will print our Current Dictionary. D1.
This is our Current Dictionary.
Now we will use the pop method to remove the values from it.
Pop method is used for what? It is always used to remove the last value.
D1 . (dot) pop.
Print Updated Dictionary.
Let’s see what happens.
Why is our updated dictionary giving an error?
A message popped up saying pop expected at least 1 argument, got 0.
Always always remember 1 thing here.
This pop is not the same one as in the list. Clear?
It removes the last value only when we use it in the list.
But when we use it in the Dictionary, you will have to pass a key.
Why? Because it stores this in Key & Value pairing combinations.
Now suppose I pass 2 here, it will remove the Mango key from here.
This was our current dictionary. But our updated dictionary is this right here.
Clear?
There are other methods just like this…
Next method is the pop item method.
Using pop item.
So what does a pop item do? Let’s see.
Which is our current dictionary? Our current dictionary is this.
D1 . (dot) pop item method, under which we will print our updated dictionary. D1.
Here it picked out the last value. Meaning it popped the last value.
To make one thing clear, when you want to remove the last value, you’ll use Pop item.
And when you want to remove value through a particular key, you will use the Pop method. Understood?
Another method is using the Del method.
Which is our Current Dictionary? This one.
Now I want to remove it using the Del method, how will we do it?
D1 and we will pass a key here, like I passed 1 here.
Print Updated Dictionary.
See, the key and value on 1 got removed.
This is how a specific key is removed using the Del method.
Finally, our last method is Using Clear Method.
This is our Current Dictionary.
Clear Dictionary will clear the whole dictionary up.
Now I'll print Updated Dictionary.
It is all blank, see?
By using these methods, you can remove values from the dictionary.
Suppose I want to access some specific keys in the dictionary.
How will I do it?
Only & only the key should be printed.
Access the only keys from the dictionary.
Let’s see how to do it.
I’ll take a variable named K. Our dictionary is already available.
Now I want to print the dictionary keys.
D1 . (dot) keys method.
Print keys K.
You can see that all the keys got printed here.
All the keys 1, 2, 3, 4, A.. all the keys got printed in the List.
We can also print the values in the same manner.
Values from dictionary.
I’ll take a variable V here. D1 . (dot) values.
Print dictionary values. V.
Run it & see only the values are brought out.
This is how you can work around keys & values.
In the next video, we will see how to use Function in Python.
Thank You guys & see you in the next video.
Share a personalized message with your friends.