Dictionaries are built-in Python data structures that store data in key-value pairs, where keys are unique identifiers that allow efficient access to values, while values can be duplicated. Dictionaries are created using curly braces with key-value pairs separated by colons, and support operations like adding new items, updating existing values, deleting items using pop() or del, and iterating over keys, values, or both. The get() method safely retrieves values without raising errors for missing keys. Sets are unordered collections of unique elements that automatically remove duplicates and support mathematical operations like union, intersection, and difference, making them ideal for removing duplicates from lists and finding common elements between collections.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
May Python Bootcamp Day 7Added:
Hello.
Hello everyone.
Hi, Mia. This one day So guys, welcome back everyone. Today we are learning, we're going to learn dictionaries, sets and the key value logic.
So let me put out the heading first.
So today we are learning dictionaries set [snorts] and the key value logic of dictionary.
Okay. Now these concepts [snorts] are extremely important guys because modern applications use them everywhere.
Okay. In login system databases are there who works on key value pair radius is there radius uh no SQL APIs they work on this format we provide data in a key value pair JSON data JavaScript object notation data we provide it in in the similar format AI projects in web development data analysis. So by end of this session guys, you will be able to build a contact book program using Python. Okay.
Now let's come to our topic that is what is dictionary. Okay. So in simple we can just simply say that a dictionary is one of the data structures of Python which enables us to store data in a key value pair. Okay. So dictionary is also one of the data structures which is by default present in Python or you can say inbuilt present in Python which enables us to store data in a key value pairs. Okay.
Or simply we can just say that a dictionary stores data in or let's say let me be more specific in a key value pairs. Okay.
Let me make it a comment.
Now what do you mean by when I when I say a key value pair? Till now we have seen list, we have seen tpples. Okay. So what we were doing in list and tpples.
Okay. We were just simply storing our values. Okay. So there was no concept of keys in those data structures. We were only focused on the value. How the value looks like, how the value is. So elements there were only elements and the elements were of different different data types. Okay. So mainly we were focusing on the values. But when we talk about a key value mean it means that obviously we are going to store the value but along with the value there should be a name uh a target value okay attached to it so that whenever we have to use that value or call that value or let me be more specific if you want to access that value so you are going to use that key it means that value is stored with the help of or with the reference name of that key. So whenever you have to use that value, you have to target that value, you need to mention that key. Okay? So a value is attached with a key. So tell me one thing guys, there could be several Python channels.
There could be multiple push. Okay? So how you are going to target me? Okay? So you will target me that PU CL coding Python channel. Okay? So this is one thing CL coding makes me unique. So CL coding is what in this situation it is the key. Okay. So that is the thing here that is the thing that if I'll give you a normal definition so I am going to write my name but this is my name but by reading it out no one will understand that what I'm trying to do here. Okay.
So let me be more specific. I'll say name is P. Now it is clear that what is fuse? This p could be a road name, a street name, a person name or it could be any other names. Okay.
So to make it more specific that it is a name name of a person. So what I've written there name. So now people will or everyone will understand. Now again let's take one more example. I write 22.
Okay. Now what is this 22? It is a number. Okay. But why 22 needed here?
Now 22 could be a normal number. It could be a age. It could be a house number. It could be anything. It could be a address specific value. It could be a coordinate. It could be anything.
Okay. So then I have to mention here that okay it is age. So now this number has got a value. Initially it was just a number and we were not clear that what this number is denoting to. Okay. Or in what reference we have to use this number that was unclear. But now when I written just age 22. Okay. So it is now it gives a meaning to that number 22 that okay this 22 is now defining or saying that it is the age of a person.
Okay. Same thing.
Now let me write Pune. What is Pune? Now everyone knows it is a famous place. So you know that Pune is a place. Okay.
Pune is a city. But for now let's forget about it. So then how anyone will know that in what proverb I want to write Pune here. Okay. So it is a city name.
So I have to write here it is a city. So this is what a key and a value means that you have written the value.
Initially in tpples in list we were just storing the values. Okay. So ideally the main logic what we were using there was that every element should be interrelated with each other. Okay. So that one single variable means one name of that list. So denote all the mean suppose if I'm creating a fruit. Okay.
So if I'll name my list as fruits. So every name or every element inside that list should be a fruit only or should be a fruit name only. Okay. Specific to that it will be a quantity of fruit or irrespective of that. Okay. Now in dictionary we get one hand forward. It means that suppose if we are creating a dictionary so we can be more specific that what is that fruit? What is the fruit color? What is the quantity of that fruit? How much price of that fruit is. So everything we can store there.
Okay. With attached to its value and there will be a key defining that value that what is this? Okay. So that is why dictionaries are important and this is the thing why dictionaries are used because it clarifies your value more quickly more deliberately more nicely so that persons easily understoods and not only persons databases also easily understands that okay what we are trying to do or what u um users wants to say okay so that is the main concern here.
So now we'll see with that with this reference now we'll try to create our first dictionary in Python. So let's just do that. Okay. So let's create a dictionary. So I'll create a dictionary for a student.
Okay. Now remember whenever you have to create a dictionary you will use the curly braces. Okay. Again I'll say for creating a list you have to use square braces. For creating a tuple you have to use the normal parenthesis round braces.
And to create a dictionary guys you have to use the curly braces. Okay.
And to store the values inside the dictionary what you have to do you have to follow some approach. First you have to write the key then you'll give a colon and then you have to write the value.
Now this is one item. So one key and value together form one pair and that one pair is called as an item. So this is a name is a key p is the value they both together makes a pair and that pair is known as an item. Okay. So one item then I want to add one more item that is age. So let's just do the okay. So age let's say 24 or let's be more specific what we have taken as an example we'll store that only okay city city is there let's what was the city pune so let's store pune okay so now you'll see our dictionary student has three items in it okay name age and city name age and city and then there are three values val also. Let me just print our dictionary student and let's just see how it looks like. Okay. So you will see you have got all the three elements and they are all inside the curly braces which denotes that it is a dictionary and the key and the value also denotes that it is a dictionary only. Okay. So this is how you are going to create a dictionary and this is how your dictionary is going to look like. Okay.
So it's very clear it's very easy. Okay.
Now to separate two items. Okay. Suppose you have one item and you want to create one more item. Okay, or you want to store one more item in your dictionary.
So what you can do? You have to just give a comma. Okay, it will not give the comma now. So then it will be a syntax error. It will say invalid syntax. So you have to take care of this thing that to separate between two items you need to give a comma. Just like what we are doing in list to separate between two elements, we were giving commas. Same thing we were doing it in tpples. Same thing we have to follow in dictionary.
The difference is that there we were only storing the values. Here along with the values we are storing the key. One more thing which I just like to tell you is that there is a column. Now this column is acting as a separator between the key and the value. Okay. So left hand side of this column is the key and right hand side of this column is the value which we store. Okay. Now I I just want you guys to just remember this thing. Okay. Because there are few persons who get confused that why value not in right side or left side or why not key in the right side because it it's a simple thing when you create a variable and you try to store a variable why always the variable name in the left side and why the value is in the right side because it is normal thing okay it is what it is followed by because values you are going to store it in the uh the variable same thing we are doing here the values are going to be denoted by the key so First we create the key and then first there is a container mean suppose if you have to store water. So first you will bring the container and inside the container you're going to put your uh water. Okay. So that is the thing you you just cannot bring the substance and try to store it somewhere.
No first you need the container the beaker you know you need the beaker first and then you will put the substance in that beaker or in that container. Okay. Same thing is followed here. First you create your container.
So variable is what? Key is what? Key is actually the container. Okay, the name which is going to define your value. So first you need to create the key. No, then only you can store the value. So these are few basics thing. Okay, people get confused in this. I don't know why but okay, it's okay. It's a general thing. Okay, I have explained it. So uh good day everyone. Yes, good day everyone. I hope you guys are liking it.
Okay, so this is how you create a normal dictionary. Now as you have created the dictionary. So now I'll tell you how you are going to access the dictionary values. Okay. So let's just do that.
Okay. Let's just do that. So we have a key uh we have a dictionary called as student which has several keys name age and city. So let's say I have to access Pune the city. Now let's say let's just start with I want to access the name P.
Okay. So let's just see how we can do that. It's very similar to how we do it in list and tpples. Okay. But irrespective in list and tpples what was the thing in list and tpples you have to write the position of that element of that uh value here there are no such thing called as position or indexing. So here keys are the only way you can access the values. So keys are your whole and soul of accessing the values.
So you have to just directly write the key name and that's it. So you'll write name and you will get the value. So first you have to write the dictionary name then followed by the square braces.
A square braces makes us in able to access go inside that and search for the particular key and then you have to write the key and remember if the key is in a string you have to write the key in double quotes. You cannot just simply write name. No it will not uh it will not work. So in whatever manner means if the name if the key is a integer value so you write integer just how you write integer value. If it is a float value you'll write in float if it is a string value so you have to write within the enclosed clothes. Okay. So this is one thing which you have to just I say remember it only. Okay. So student name and you are successfully able to access the value of the key name. Okay. I'll copy it. And let's say if you want to paste the city. So let's change the name to what city name city key name. So now you'll get the value of city that is Hune. So we are getting Hune. Okay. So this is how it works. Okay. This is how it works. Okay. Now this was how you can create a dictionary. How you can access the values by creating a dictionary.
Okay. Now guys, uh one more important thing. Okay. Which I like to add here in dictionaries is and which is very common also. Okay. Um see values in dictionary can be duplicate also. Okay I'll give you an example.
Okay I'll give you an example then it will be more good. Um okay I'll do what I'll give road name.
Okay road name and I'll give the road name as 22. Okay just for example purpose just assume it. Okay, don't go that okay this is wrong and don't go that much logical I'm just for example I'm giving you okay now I just run this cell now you will see name P is 22 city Pune and road name 22 it means values can be duplicate okay so in dictionaries you can have duplicate values okay but but you cannot have duplicate uh you cannot have duplicate keys. Okay.
So, one thing which I like to say here is that in dictionaries values can be duplicate but keys are unique. Keys cannot be duplicate. Keys will be unique only. Okay. So, what will happen if I have a key which is already present in my dictionary. Okay. So, what is going to happen? So, let me show you what will happen. So now you guys will see that age is already there and age value is 22. I again added age and now this time I given the value 24. Okay. Now let's just execute this cell and let's just see what happens. So name push age 24 then city Pune road name 22. Okay I have two times age but I got only once but there is a change.
There was 22 but we are getting 24.
Okay. So yes that is the thing guys if uh keys are always unique it means that if you are trying to give a duplicate key also okay if you are trying to give one more key with the same name so dictionary will take it as an updation.
Yes dictionaries will think dictionary will think python dictionary will think that okay you want to update the previous key with the latest value so it is going to update the value. So initially we have already age and the value of age was 22. It found out that one more age is there with updated value 24. So what it does it just actually updates the value of the previous age because unique keys are unique in nature. So duplicate keys are not allowed. So because of that it just updates the previous one with the latest value. So it just thought that okay it does not ignores it. It considered it but it only considered it checks that okay age is already there. So it just check that value is different or not. So when the value was different it just updated it. If the value will be same so it will be 22. Now this is also the updated 22. This is not this 22. This is the last 22 only. But because they are same value so you cannot say that which one is that. But actually logically it is the updated 22. Okay. So that is there. I hope you guys have now understood that what is going to happen if you give a duplicate key. It's not going to give you an error. It's not going to do anything. It's just only going to update the value. Simple as that. Now I hope you guys have got it.
This is one thing which I just I wanted to tell you guys. Okay. Now the same logic will be applied everywhere. Okay.
When you'll use the dictionary methods and all then the same thing will be applied and we'll see in this session only. So no need to worry. Uh so this was one way of accessing the values. Now another way of accessing the values is by using a method and the method name is get method. So one more way of use uh accessing the value is using the get method.
Okay. And what you're going to do it's very simple. First you need to have a dictionary with you. A dictionary object because it is a dictionary methods. It will be applied to a dictionary object.
and just write dot get and the same thing just like how we are doing here you have to just write the dictionary key name same thing here you have to do you have to just write the key name let's say age okay it was a student it was not a students I need to remove the s now you'll see you get the updated value of the student age that is 24 so you got 24 okay so yeah that is there and one more difference between this technique of accessing the key and this key this way of accessing the values.
Okay, the difference is very clear. I have shown you that as you can guys as you guys can see when I try to access a key using this technique but that key is not there. Okay, means suppose name one.
Now you can clearly see name one is not a key name in our dictionary student but I just tried to access it. Okay, means I just tried to access a key which is not present. So what happened? We got a key error. Okay, we got a key error and it mentioned that key error name one. It clearly means that name one is not there. It is a key error. But when the same thing I did with get method means I try to access age one. Now age1 is not a key in our student dictionary but it gives us none. It does not gives an error. So it means using get method is safer than direct indexing because it does not give errors. Second it avoids errors. Okay that we have clearly seen.
Okay. So this is how it works and this is how you're going to do it. Okay. So yeah, so these were the two methods. Now get method has one more uh unique thing that I'll tell you later on. Okay, I'll just add it and I'll just tell you that how you have to do it. But yeah.
Okay. Now guys, we'll see that if you have to add a new data in your dictionary. So how you're going to do that? Okay.
So if you have to add a new dictionary or a new item in your dictionary. So what are the ways in which you can add it? Okay.
So now we'll look out at that.
So for that guys you have one normal simple way and then you have methods given to you. Okay. So let's just see all the techniques how you can do it.
So one way is one way is let's say uh we have the student dictionary with us and now student if it is a if there is a student then he must be reading something he must be following some course. So let's just add the course.
Okay. So if you are trying to add the course okay so what how you're going to add the course in your dictionary student okay so this is the way how you are going to add okay Now guys you will see course MCA has been added. Okay.
Okay. Mina this funday has one question why it is writing age one and name one to show you the difference between using get to access the values and using using normal technique. How what is the difference that here you get a error but here you don't get an error you get none if the key is not present. Okay. Now guys, if you have to add an element or add an item to your dictionary, so what you can do is you have to just write the dictionary name and whatever the new key you want to add, write that and give the value for that. Okay, then print the dictionary. Dictionary does what?
Dictionary checks for that particular key course and then it will see that okay course is not there. Course is previously there is no key called as course. So it is going to add that course and the value of that key course as an new item and whenever dictionary adds a new item it adds in the back.
Okay, it adds at the last of the dictionary at the end of the dictionary.
Okay, so this is the way dictionary follows just like how indexing is there, positions are there. So this is the way of dictionary. The dictionary always follows the same technique in which the items has been added to it. Okay. So, dictionary will be in same ordered. So, dictionaries are actually ordered in nature. Okay. Ordered in the sense that in which sense they are ordered.
Whatever the way you have stored the dictionary items, it will be in that order only. It will follow that order only. And rest of the items which you are going to add, it will be added in the end of the dictionary. Okay. So, dictionary is ordered in nature. Okay.
This is also one thing which you have to remember. So this is how you're going to add add new data to your dictionary. Okay.
Now for updation I have already shown you that how you can update values. But again I'll um let you know that how you can update value. So let's say what are the things which you can update. Now let's say road name. Let's update the road name from 22 to some real road name. Okay.
So let's give MG mark Mahatma Gandhi mark. Okay.
And not like this. Okay. Pardon? It will be actually like this. Yeah. MGM mark.
So if you have to update value, how you can update the value? Like this. Print student And let me just execute this. Now you'll see guys name, age, city and in road name the values updated. So what happens in dictionary that if you have a key which is already present in your dictionary. So now it will update it with the updated value. Okay, it just updates it. Okay, simple as that. If you give a new key, so it will be added as a new key value pair. If you give the same key so it will be updated the previous key and value previous key and the value of that will be updated. Okay. So I hope you guys are getting it. Okay.
U Okay. So this is how it updates. And then one more way of adding an item is adding new data using there is one more function which is called as update.
Okay.
So there is one update method which also you can use. So let me show you how you have to use that method. [snorts] So you have to just write update and this inside this update you have to give a dictionary. Okay. And then you can list all the items which you want to add in this. Okay. So if you're talking about a student so a student will have a role number. Let's try to add a role number.
So just like you give a dictionary element you have to just write it in same format. RO number let's say 001 or let's say 01 or let's say one only.
Okay. And let's just print a student.
So now you'll see wrong number and one has been added successfully. Okay. So this is how update works. And inside update you have to give a dictionary only and then you can add as many as elements you want. Okay.
Now you have seen all the ways of adding an item. So let's see all the ways in which how you can delete the items.
Okay.
So let's see that. So first of all we'll start with the pop method. Pop method is very simple. Okay. You have to just mention pop and inside pop you have to mention the key name. So let's say you want to remove road name. Okay. So just write the key name road name and that's it.
Print a student and you will see that road name is no longer present in your dictionary students. Okay. So when I printed a student you'll see name, age, city, course, role number. Now road name is removed. Okay. So this is pop and if you not write anything in pop. So pop will do what? It will remove the last element. Okay. So let me just show you that also.
Now I'll run this. I'll not put anything. Now you can see in the end there is wrong number. Okay. So now there is nothing in pop. I'll print it.
Okay. Pop expect at least one argument got zero. Okay. So pardon that's not uh you cannot do it in dictionary. That is possible in I guess list that it removes the last element but not in dictionaries. Okay. So yeah.
Okay. Then there is one more way and this is using the Dell keyword. Okay.
Now comes the interest interesting part.
Now I don't know how many of you are aware of this keyword but yes there is specially a keyword that is for deleting. Okay. And that keyword name is Dell that stands for delete. Okay. If you have to delete a specific uh specific key, so you can delete it using this also. Okay. So what you have to write dell space the dictionary name and then you have to write the key name.
Okay. So let's say the key name which you want to delete is ro number. So just write ro number and just give a print to just verify that whether it has been deleted or not. So now you'll see role number is no longer there in your dictionary. So it is deleted. Okay.
And then uh there is one more method.
The method name is clear which is used to just clear Okay. Uh S3 YouTube channel pop. Yes.
Yes. Yes. Thank you so much. Uh method remov please. Yes. Yes. Yes. Correct.
Correct. So okay.
See one uh one one guy just tell me he just remember me about one of the functions that is pop items. Now this method is used to remove the last element. Okay, which I was trying to do with with pop but actually it is done with pop items.
Okay. And one more specific thing which I tell about this method is that after the update of 3.7 version 3.7 Python update uh it started deleting the last item before that it was deleting only the uh random item.
Okay. So before the the before the 3.7 versions of Python this pop item used to delete any random item. Okay. But after the update, fixes were made and then it deletes the last item. Okay, so let me just show you.
So now the last item is course MCA.
Okay, so you guys have to just remember now the last item is course MCA. And I'll do what? I'll just copy it out and I'll paste it over here and I'll just add items and I'll remove this part.
Okay. So now you will see I told you know that course is the last item. Now there is no course because it just removed the last element. Okay. So yeah then there is guys then there is uh clear. Now this clear method is present in everywhere mean in all the data structure. So you can use it in list also you can use it in tpple also and you can use it in dictionary and later on when we'll talk about sets in sets also this clear can be applied. Okay. So this method is has only one job to do that is to clear everything from your dictionary. So it is going to remove everything from dictionary because now in a dictionary name, age and city are left. When I'll use this student dot clear, it's going to remove everything.
So to verify I'll just print student.
Now you'll see you have a empty string.
Okay. So it just removed everything from your dictionary. Okay. But but but one thing is there that guys it removes everything from the dictionary but still a student dictionary exists. Okay still a student name dictionary exist. Yes one thing is true that now that dictionary name student is empty. There is nothing inside the dictionary but still the student dictionary exists. Now guys, if you want to delete the existent of the dictionary, if you want to delete the existence of the dictionary, so you can do that also. Means suppose if I'm trying to print a student, so you will see I'm getting a uh this it means somewhere in the memory student dictionary still takes some space. Okay, if you want to delete that also means if you want to just delete the whole existent of the dictionary named student. So then you can use the Dell keyword. what you have to do then you have to write the dictionary name student and then you'll try to print a student and it will give you an error okay so now you can see first it prints the empty dictionary then I just deleted the student then I tried to print a student it gave me a name error that name student is not defined so now after this line is executed if you try to access a student no so there is no more a student object in your system so it has deleted all the occurrence of a student. Okay. So that deleted the existence of the student dictionary.
Okay. So this is how you can delete that also. So yeah, this was also one thing.
Now guys, we'll see some looping how we can approach or start looping in a dictionary. Okay. So let's see that and that is also very unique for dictionary.
Okay. Actually not unique little bit changes are there in for dictionary.
Okay. And I've already told you that for loop is basically optimized for the data structures. So yeah that is also there.
So now I need to do what? I need to do one thing that I need to again put this out and I again need this dictionary because I need some dictionary. So okay I'll remove this part.
Now student dictionary again I have my student dictionary. So now I'll start looping in it. So to start the loop in a dictionary just like you do it in a list you have to do it the same way. So print I.
Okay. So you get name is city and road name. But wait a second I'll do what?
I'll make I I'll remove I and for you guys I'll make it key. So then it will be more understandable for you guys.
Okay. So now you'll see guys you when I'm just normally writing the loop just like how we are doing it in list. So it just iterate over the keys. Okay. Not the values. It just iterate over the keys. Okay. Okay. That's okay. That is the thing. Okay. Now what if if you want only the values then then what we should do? So for that also there is a thing for that there is u one thing called as a dictionary method is there. Okay.
Which you can use to iterate only over the values. Okay. So yes you can do that. So for value in student do values dot values is the method which is used to extract only the values from the dictionary and then print one by one each value. So now you'll see it will iterate over all the values. Okay, it will iterate over all the values only.
Okay. So now we see that you can iterate over the keys. You can iterate over the values. But if I have to iterate over both I want the key and the value both then how I'll do that. Okay. So for that also there is a method. Okay. And the method name is items. But to use that method you need two loop variables because here you have to iterate over two things at the same time. You want to iterate over the keys also. You want to iterate over the values also. So you will need two loop variables. So for key comma value in student dot items print key then give a colon or you can give a arrow and then you can print the value.
So now you'll see name views age city road name. Okay. So in this way you can iterate over both key and the value.
Okay.
Now with that said we have covered almost everything about dictionary which a beginner needs. Okay. So now I'll give you small examples. Okay. Small nice examples. Okay. And let's see how it works. Okay.
So what uh I want to create a product price lookup system, a small um product price lookup system.
Okay. So for that I need a dictionary first. So I'll create a dictionary of products laptop and let's give a laptop a price 50,000 mouse 500.
So I'm giving a random price guys. Okay.
So don't quote me on that that why we have given this price we cannot get a good laptop in this price. Okay, I know it but I just for example purpose okay it's not something you have to take it seriously.
So now guys we have a dictionary okay of products where I have mentioned the products and product and the price of that products okay now I want to create a system which will ask me for the product name I'll give the name and it will give me two things either if the product is there if that product is there present in the product list so it will give me the price of that item of that product otherwise it will send a message that product not found. Okay, so there are two things either it should say that the price of this item or it should say product not found. Okay, so I want two things here conditional base.
So how we are going to do that? Okay, so let me just show you. It's very easy.
First of all, whatever the input you want to take for the product or the input you can say first take the input.
So I'll just use the input function and I'll say enter the product name. Okay, that's nice. Then I'll use the print function. Inside the print function, I'll use the dictionary because whatever the search I want to make that is in dictionary in the products dictionary only I can do it. And then I'm going to use the get method. Okay, because get method is only the thing which I can use for a conditional based asking. Let me show you how. So first of all I'll give item because item is the input which we want to search for. Okay. So I'll just directly pass in item there. Okay. And we have seen the get method. Get method takes input of the key. So we have given the key name. Then one more thing which I want to tell you about the get method is that get method takes two parameter.
First it takes the key name. Second it takes the default value. So it will not give the default value and if the key is not present. So we have seen it will give you none but if you want to give a customized message so you can give that customized message also. So that message I will give you here that product not found or product not present or anything like that. Okay you can give it. Now I'll enter the I'll run the cell and it will ask me for a product. I'll search mouse.
It will give me the mouse value 500.
Then I'll do what? I'll search for keyboard. So keyboard is there. So I'll search for a monitor.
So product not found because monitor is not there in our dictionary products.
Okay. So this is how simple it was and so efficient it was.
Okay. I mean so easy.
Okay, I'll show you one more example like this. Okay, uh let's say we have to create a word counter. Word counter means that we will enter a sentence and in that sentence whatever the alphabet how many times that alphabet is present, we have to tell that. So that is word counter. It is going to count each word that how many times that word is present in that sentence. Okay, so it will give me a total whole dictionary about each word. How impressive is that? Okay. So let's just create a word counter using dictionary and let's see how we can do that. Okay. And I can bet it if you guys know it then you will know that how easy it is. And if you don't know then now you will know that how easy it is.
So I'll just simply say enter sentence.
Okay. And then I'll do what? I'll create a um a simple text dotsplit.
Means I'll do what? I'll whatever the text I'm taking as a input I'll apply the split functions to it. Split function will do what? It will automatically split all the words in a list format. Okay. So I have just and I store it as words. So now we will have all the words as a list in stored as words. Okay. Then I'll create empty dictionary called as count. So I'll create empty dictionary called as count.
And in this count dictionary only I'm going to do everything. So first I'll start the dictionary for word in words.
Okay. For word in words I'll start the loop in our list words. Okay. And then I'll do what one concept of dictionary which I have told you just now is if the dictionary gets some new gets some new key. Okay. So it store the new key as a new item of the dictionary. But the same dictionary if it gets the a key which is already present in the dictionary so it updates the value. Okay. So we are going to use that approach. Okay. That technique we are going to use it. I'll write count word. Okay. So each word will be will be taken as a key of my dictionary count. Okay. And then I'll do what? I'll say count.get. Okay.
Count.get what? then I'll search for that word in my dictionary. Okay. So two things will happen. If that dictionary and I'll give give if a dictionary is present, if that dictionary has already the same key, same word key present. So it will give you a default value zero. It means if it is a unique value, so by default value zero will be stored there. Okay? And not zero. I'll do what? I'll make it + one.
Okay? So if the key is not present so 0 + 1 it means one will be stored there means first time this key this alphabet has occurred so one is the value then in the second time if that key again comes so it will do what this thing will run now it will give the original value of the word so let's say a is the value which is already present in my dictionary count and the total value of that occurrence of a is one and one more time a comes. So it will return this word will give you one and then one will be added it will make two and two will be updated so it will store two.
Now we have to just print our dictionary count and that's it and that's it.
Uh yes brother it may happen so that my voice will be low. Okay, wait. I will increase the volume little bit but the volume is full. Actually it's full. The main reason is that actually I'm traveling right now. Okay. And because of traveling I cannot carry my whole system. Okay. And that's why I have carried some different microphone which is sufficient enough to carry. Okay. So because of that uh maybe the voice could be low. I just suggest you just increase your volume a little bit so that you can match up my voice. Okay, please do that convenience. Okay. Uh yeah, that's the thing. I have to finish this boot camp because it was a May boot camp. So, it should be finished in the May month only. So, yeah, that's the reason. Um I'm sorry for the inconvenience code.
But please, if you may increase the volume a little bit, so that will be nice. Okay. Uh so yeah, your our code is ready guys. Let's run this. It asks me for a sentence. So I'll just give Python is a programming language.
So Python is one time is is one time. A programming language. Okay. Now let me just give programming only.
So for programming it gives Okay. Uh let me give a very big sentence.
There was a king who was very rich and because of that he died.
So now you'll see there is one time was were two time a was on king and everything else was one one one time. So was was two times. So yeah that is how you create a word counter. Okay and it working it working nicely. So this is how you create it using dictionary and so easy it is. Okay. So this is how you have to do it. Okay. So this was all about the dictionary. Okay. Lebanon Abdul says your voice okay. Okay. Thank you. Thank you brother. Void 9 please increase your volume little bit. Okay your volume. So it will may help you.
Okay. Now guys dictionary is all set.
Okay, we have seen everything about dictionary. We have seen few programs also how we have to approach on dictionary that we have seen. Okay, so now it's time for sets. So let's come to the next topic of this session that is set. And set is also one of the mostly widely used most widely used thing in the Python that is for its nature. Okay, sets is also the one of the built-in data structures present in Python but it is totally different from list and tuple. Okay, dictionary is a different thing because it is a key value pair.
It's more like a JSON kind of thing. But set is pretty much similar with list and tpple. But in some sense it is very different. The use cases are very different. So let's talk about that.
Let's see sets now.
So now it's time for sets. So let's give few time to sets. So guys sets as I've already told you it is also the one of the data structures present in Python. Apart from that a set is a set is guys unordered.
Unordered. Yes, it is not ordered. There is no indexing. There is nothing. Okay.
It is totally random. So it is unordered in nature. But it is unique set of collection. It is a collection but it is unique collection means it it stores only unique values. So no duplicates values are allowed. So no duplicate values are allowed because of its uniqueness.
Okay. How we are going to create a set?
Let's see that. So to create a set, let me create a numbers.
1 2 3 4 and yeah that's it guys. That's it. And now you guys will be confused and tell me that okay again you are using curly braces. Yes. U curly braces. Curly braces are also used for creating sets not only for dictionary but for sets.
But there is a difference when you use the curly braces and when you give only single elements so then it is considered as a set. Otherwise it will be considered as if you'll give key value pair it will be dictionary. If you give only single element so it is a set.
Okay. So this that is the different different is the way you give the values. Okay. So yeah this is how you create a normal set. If you want to check the type of it, you can check it will be a set not a dictionary. Okay, you can see it is a class set.
Now let's try to create one more set.
Okay, so I'll do what? I'll create a set called as nums and I'll give random.
Okay, not like this. I'll give random values, duplicate values, few. Okay, like this. And let me just try to print nums now.
So guys now you'll see one thing here is that uh I have given two fours 2 6x okay but it gives it considered only one 1 4 and 1 six why because it only allows unique values it do not allow duplicate values so because of that be difficult for you that because anytime any value can come it's not compulsory that one will come first only no it's not like that any value can come at any time.
Okay. But there are few operations.
There are few methods which are allowed in sets. So in sets what you can do? You can add values in sets and you can remove values from sets. Okay? You cannot modify a value. Means once a set is created you cannot do modifications.
Means if two is already there you cannot change two to 20. That is not the thing which you can do it. But you can add three in that. Okay. In this set three is not there. So you can add three. You can do that but you cannot change any value. So modification is not allowed but editing is allowed. You can add and remove that is you can do it. Okay. So let's see how you can add and remove elements. So there are few methods for that. For add there is a method. For remove there is a method. So let's just look out at those methods and let's see how it works. So 1 2 3 there is a dictionary. I want to add okay four in that. So how we'll do it?
There is a method called as add. Just write four inside add and print nums.
Print nums and 1 2 3 4. This is what you are getting. Then again for remove also there is a method called as remove.
So let's see that method. Uh again nums dot remove. And again you have to give the value name uh let's say four. So four print nums.
So you'll get 1 2 3 4. Okay, you will get 1 2 3. Okay, four is removed. Now again if you try to run this, you'll get a key error called as four because four is now removed. You cannot remove four again. Okay, that's the case. So then there is one more thing called as discard.
And if I write four. So and I'm using okay now discard also removes element okay discard also removes element just like how remove removes element discard also removes element but the difference is that once the key is not present means once the value is not present in the set and you are using remove to remove that particular value it will give you a say that that particular value is not there that particular key is not present so you cannot remove it irrespective of that discard also removes the element Okay. But if that key is not present mean if the value is not present in set so discard will never give you an error. Okay. So this is more secure because it does not give any error. So that's why it avoids error. It does not avoids error. So both the use cases of remove and discards are same irrespective of the error. If the key is not present, remove will give an error.
If the key is not present, discard will not give you an error. Okay. So that is the difference. Let's see some few other methods that is one method is union.
Okay. Now union means u it will combine two sets. Okay. So let's say and these are the some of the set operations. So set has some operations because set is a term which is taken from the mathematical sets if you guys know it.
And in mathematical sets there is union, intersection, symmetric difference that kind of things. So that concept is being taken here because sets are in mathematics the sets which we know that also is unique in nature. Okay. And this set is also is taken means it considered from that only. So it is also unique in nature because of its uniqueness. Those operations can be performed here. So that's why we have few methods here which is also known as set operations.
So one by one we will see all of these operations. Okay. So first operation is union. So for that I need two sets with me. So I'll create quickly two sets 1 2 3 and 3 4 5. So these are the two sets.
And you will see that one element is common in both 33. Okay. So let me just do what now? I want only the unique elements from both the cells. I don't want the duplicate values. Okay. I want only the unique. Okay. you no unique only the unique means if there is two three I only want the first three I don't want the duplicate three okay so then I'll do what I'll do union so I'll do a dot union of b so this will give me 1 2 3 4 5 there should be 2 three but because it is union so it removes the duplicate values so we get only 1 three so this is how union works okay then there is intersection Now intersection will give you only the common values. So when you want only the common values, so intersection is the operation which you have to perform. So intersection this is how you do it. So I'll do what?
Print a dot intersection B. So now you'll see it will give only three. Why? Because three is the only value which is common in both the sets.
set A and set B present in both the sets. That's why you're getting three here only. Okay. So this is intersection. Then there is difference.
Then there is the next operation which is difference. Now difference is used for give you the unique value of a specific set. I'll show you how. So if I'll write like this a difference B it means I want all the unique values from set A. So what are the unique values of set A? 1 and two because three is duplicate. So one and two. So I'll get only one and two. I'll get one and two.
Okay. So what if if I want all the unique values from set B. So I can do the vice versa. I'll do what? I'll change A to B and B to A. So now I'll get all the unique values of set B.
Okay. So this is difference.
I hope you guys are getting a point what I'm trying to explain. So these are the only few things which you need to take care about set that set are unique in nature. So duplicates are not allowed.
If you give a duplicate value also it's not going to give you an error but it will consider only the unique means it will consider the only first value. Rest of the duplicates it will automatically be removed. Okay. So that is the case.
Okay. Uh if that's the case, so now I have few examples to you for you guys.
Let's see how many of you like it and how many of you will solve it. Okay. So let's say you will get a problem that you have to remove duplicate names.
Duplicate names. Okay. And for that you will give you will be given a list of duplicate values. Okay. So let's say you have Rahul then you will have Ammon and again you will have Rahul then you will have Ankit. Okay. So you have a list of names and there are few duplicate values present in this and you have said to just create and store means remove the duplicate values and keep only unique names. So what you can do?
Okay, you can search for one particular value and then you can search that that value exist in that list or not. You can go and follow that approach. Otherwise, you can use the set constructor. So, how you'll do that?
Unique names. First just create a unique names variable. Then use the set constructor and pass the list name in it. Okay. And then what will happen now?
uh then just print unique names and before give you before showing you the output I'll just tell you what is happening here what we are trying to do is first of all we have created a set okay and we have used a set constructor now what is set constructor set constructor is a function which is can convert any iterable to a set okay so list is an an iterable and because we have given list as a set so this list names will be converted to a set And because it's being converted to a set so all the set property will be applied to that list. So first set property is that it will contain only unique elements.
Second is that it will remove all the duplicate values. So this property will be applied because we are converting this names list as a set storing it in a new object called as unique names which is now a set. And when you'll print the unique names you will see you are getting curly braces and all the unique values. Okay. And I have already told you that there is no uniqueness. There is no ordered. It is unordered in nature. That's why you will see you'll get Ankit, Rahul and Ammon. If it will be ordered, it should follow the same order. But actually it is random. So Ankit, Rahul and Ammon. Okay.
Now uh let's let's let me give you one more example. Uh we have to find common friends. Okay. We have to find common friends. So I have one user I have one user who has uh these many friends.
Rahul is the friend of the user one.
Aman is also the friend of user one and Priya is also the friend of user one. Okay. Then I have one more user user two. Okay. So Priya is also the friend of user two.
Nha is also the friend of user two and Rahul is also the friend of user two. So our task is with the question it is simple that we have to find the common friends of user one and user two. So how we are going to do that? So it's very simple we know intersection we have to do intersection here. So user word dot user one dot enter section user two and let's just print common.
Okay I made some spelling mistake.
Okay there will be no e. So rahul okay ID priya it is not priyas.
So now Priya and Rahul are the common friends between user one and user two.
Okay. So this is how you check it. This is how you check it. Okay.
So uh this was all about the sets. Okay guys, now I'll give you few questions.
I'll give you a few questions to you guys just for practice. Okay, so this is audience this is or audience practice problems.
Once I write these problems, you can pause the video and note all these questions okay one by one. So the first question is uh what you have to do you have to create a dictionary of five countries and their capitals as the value. Okay.
Second one is second one what you have to do is you have to count the frequency of each characters. Okay the frequency of characters using dictionary. Okay. So we have seen the word counter but now you have to create a frequency counter of characters. Okay. Then next one is you have to remove duplicate numbers using set. This is your third problem. Fourth problem is to find common values between two sets. You can take any random two sets. I leave it up to you how you are going to create uh uh the two sets. Okay. And last, okay, the main thing which you have to do it is you have to create a mini login system. You have to create a mini login system using dictionary. Okay. So it's very simple. You have to store the login ID and the password. Okay. And then you have to give a conditional statement and match the login ID and the password. If it matches it will be successful login otherwise it will be unsuccessful or the access denied. Okay.
So that kind of thing this is the main important one and I just request you guys to not use any kind of AI and not go through any kind of applications on internet because this is for learning purpose. You're doing it for yourself.
So please don't use it because I'm not going to review it. It's just for you.
It's just for your learning. What you have learned till now is just the thing you can apply it. So it's just that. So yeah.
So with that said guys, I'll just say thank you so much for joining the session. Thank you so much for making this session so much engaging and so it feels very good when you guys engage it when you guys just reply in the chat.
Okay. And I'll just say be consistent, keep learning Python, stay tuned with our channel and be focused. And if today's Python dictionaries and set session helped you that make sure to just like the stream, subscribe to the channel and practice the mini login system using dictionary.
Okay, tomorrow we'll move toward functions and modular programming. Okay, because functions are the next topics which is I guess left. So we'll follow up with functions and we'll follow a modular programming approach. how functional approach is used in solving most of the real world projects. Okay.
So I'll just say thank you so much everyone.
Uh J Charu. Okay, I'll do that. Thank you, sir. new subs here. Okay, thank you so much. Thank you so much for subscribing to the channel.
Okay, guys, I'll take a leave now. Thank you so much.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03











