0% found this document useful (0 votes)
8 views

3.5 - Dictionary.mp4

python

Uploaded by

puneeth369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3.5 - Dictionary.mp4

python

Uploaded by

puneeth369369
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

The next very important data structure in Python is called a dictionary.

So for those of you


who know algorithms and data structures, a dictionary in Python is like a hash table is like a
hash table. If you don't know what's a hash table, it's perfectly okay. For those of you who
know what's a hash table, it's basically, we will come to it. We'll come to how a hash table
works in little detail, not in full rigorous detail, but for those of you who know what a hash
table is in data structures, if you have studied this, I believe most students of computer
science study this concept in their undergraduate. So if you know what a hash table is, a
dictionary is nothing but a hash table in Python. So let's see what's a hash table and how it
works. The very important thing is a dictionary is an unordered collection of items. It's not
ordered. So the moment it is unordered, you cannot index it. You cannot index it. It's non
indexable, it's not ordered. Remember, your tuple is ordered. Your tuple and your list are
ordered. Are ordered, right? Your tuple is immutable while your list is mutable, but both of
them are ordered, while a python dictionary is an unordered collection of items. Okay, we'll
see. Let's understand hash tables and pythons in little detail. Okay? So imagine if I want so,
in a python dictionary or even in a hash table in general, in any programming language or in
general data structures, you have two values. You have something called a key and a value.
Okay? You can think of that as a table that it's storing it in. Okay? One, one good way to think
of a dict or a dictionary is you have a key and a value, right? You can store anything in the
key. For example, let's look at. This is like creating an empty dictionary and dictionaries.
Typically, as we already know, we put curly braces to represent dictionaries. So here I've
created a dictionary and I've said so. Dictionaries you create by giving key value pairs. So
this is called a key value pair. So when you say this. So this is your key and this is your value.
Okay? Similarly, this is your key, and this is your value and key and value. Between key and
value, you have the column. Okay? So let's see how it works. The moment I do this, it says,
for key of one, store ABC. For key of two, store XYz. Right? So this is what happens. It's
basically storing this data into a table, okay? But remember, let's see how this table is useful.
Right? So this is what exactly is happening when I create a tick. It's creating a table with two
columns. My first column is a key. My second column is a value. It's storing my keys in this
and my values here. Let's see, what is the power of dictionaries. Just give it a second. Okay,
now imagine, oh, by the way, dictionaries can have mixed keys, right? So here, my key is
name and my value is my first key. My k one is name, my value one is satish, my key two is
one. Remember, this is integer and this is string. That's perfectly, okay, my value two, my
value two is a list itself. Okay, so how is it getting stored internally? Internally, you have
your key value pairs like this. My first key is name with a value of satish, right? And my
second key is one with the value of ABC comma xyZ. Okay? So this is what is being stored. So
when you say my dict equals to dict, it's create an empty dictionary. It literally creates an
empty table for you. Okay? So you can also create a dictionary. You can initialize, you can
create a dictionary using list of tuples. Okay, this is a tuple, right? This is also a tuple and you
have a list. This is now basically a list of tuples. This is now a list of tuples. You can give a list
of tuples, right? You can give a list of tuples and you can create a dictionary out of this. So as
soon as you create this, mydict will have this, my dict will have my keys and values. My key
one is one, my value one is ABC, my key two is two. My value one is XYZ. Okay, so what is
this? This is a tuple and we have a list of two tuples, and you can create a dictionary out of
this. Okay? So one of the big advantages of a hash table or a dictionary is as follows. Let me
try and draw an example for you. Suppose if you're storing a data. So let's take I have my
key and value, okay, let's just say, let's assume my keys. Let's assume, just for simplicity,
let's assume my keys are name and my value is address. Okay? So I have name one, address
one, name two, address two, so on, so forth, name, k, address, K. Let's assume I have stored
this in a table, okay? Now if I say, see, this is the power of a hash table or a dict. This is
where it is super powerful. Or a dict, now let's assume if I say I want to find now if I want to
find the address, if I want to find the address of a user whose name is Ni. So here you have ni
whose corresponding address is AI, right? So typically, what do you do if this was stored as
a list of, if this was stored as a list of, list of tuples, right? I could have stored each of them as
a tuple. I could have and stored them as a list. Then I have to go one by one. I have to go
sequentially, I have to go sequentially, and I have to check one by one whether my first
name is n one, my first name is Ni or not. If it is not, I go to the next item, I check. Is it ni? No,
I have to keep doing it. I have to keep checking each of my names in sequence to find the
address of my user with name ni, right? So, which means it's going to take a lot of time. On
the other hand, a hash table, a hash table or a dictionary has a very, very nice way of storing
this data. It uses some complicated and interesting mathematics. Those of you who have
studied hash functions and things like that will understand this. So a hash table and a dict is
stored in such a way that I can say, get me the address, get me the address of Ni. And it
would get it to me without going through it sequentially, so it knows exactly where ni is
stored. So it knows exactly the row in which the Ni is stored. It knows exactly that without
having to go through each of them sequentially, the way you would have to do if this data
was stored as a list of tuples. Okay, so hash table or a dictionary makes you search for
something very, very efficient and fast. And that's why we use dictionaries a lot. If you ask
me. Dictionaries, I think, or hash tables are one of the most used data structures in the
industry. I mean, most of the real world problems actually can be solved very elegantly and
very fast using hash tables or dictionaries. Okay, so this is a very, very important point. The
dictionaries can get you data that is there in a very, very quick and fast manner, while if you
store it in any other data structure like list of tuples, it takes you significantly longer time to
get that data. Okay, having said that, let's go and understand accessing. So since we know
the dictionaries are very, very fast ways to access data, right, as compared to lists or tuples
or databases. Okay, so now let's say, let's see how to access. So we covered how to create
dictionaries. Let's see how to access elements in a dictionary. Okay, so imagine if I have a
dictionary which is name Satish, age 27, address Guntur. Guntur is, by the way, a town, a
very, very famous town for education in Andhra Pradesh. It has some of the oldest institutes
of learning in science, mathematics and engineering in Andhra Pradesh, probably whole of
India. So now if I say my dict within square brackets, if I put name, I'm saying find me. This
is the key, right? I query using key and I say, get me, what is the value corresponding to this?
It immediately gets it to me very fast and it says the value is print. Now if I say my deck
degree. Now the key here, the k one is name, k two is age, k three is address. Right? There is
no key with degree, right? So when I try to print it, it gives you something called a key error,
which means this key does not exist in this dictionary. That's what a key error means. Okay,
so now let's see what the error error says. Key error degree, which means degrees, doesn't
exist. See, by the way, the errors that python gives are terrific. They're very, very easy to
read, understand and correct. Okay, so now let's go. Is there another way of accessing the
value? Right, because we query using the key. We query using the key and we get the value,
right? So if I say get address. So the other way to do it is get dot get is a function in
dictionaries which if I give it the key, it'll give me the value. And now remember, the key
degree didn't exist. It gave a key error earlier, right? But if I use the dot get function, it will
return you none. So it's always good to use dot get because it will not raise an exception or
an error. It'll give you a value none, which means nothing exists. Okay, now how to add and
modify elements in a dictionary. This is again very straightforward and simple too. So if I
have a dictionary with name, with k one as name and value one as satish, k two as age, value
two as this, k three and value three. Now if I want to add a new element to my dictionary or
if I want to update something, now I'm saying my dict name equals to Raju. So what's
happening now? It says my dict name goes to this. Right? And the value that it has is Satish.
Right? Now it will replace Satish with Raju here. Okay? Because name already exists. It's
about modifying elements. Okay? Now when I print it, I get name equals name Raju, age 27.
Address, computer. Okay. This is about how to update. Update a value given a key, right? I
can add a new key if I choose to. So if I say my dict degree equals to Mtech. I think our satish,
our friend here has a master's degree. It's not that. I think. I'm sure he has a master's degree.
So he has said that my dict degree equals to Mtech. So now it adds a new value. So this will
become my key. Four. And this will become my value. Four. Now when I print it, I have
name equals to Raju, age equals 27. Address equals to Guntur. And degree equals to Mtech.
So this is how you add a new key to a dictionary. And this is how you edit or you modify or
update an existing value for a given key. Okay, now how do you delete or remove elements
from a dictionary? It's again very simple. If you have a dictionary like this. If you say pop,
this function pop. And if you say age. Right? If you say age. So it'll remove. It will return the
value which is here, 27. When I print it, I get 27 here. Okay. But it will remove. Now when I
print my dict, the age is no more there. So pop is a function which will return the value but
remove it from the dictionary itself. Okay, so this is the pop. There is another related
function called popitem. For popitem, there is no parameter here. Popitem will remove any
of the keys. It can remove any of the keys arbitrarily. Okay, so in this case, when I ran it this
time, it kept this key. It kept this key. It removed the last key. But it can remove any
arbitrary key. So pop item removes one of the key value pairs from the dictionary. Okay, so
let's go down and understand. There is another way to delete, by the way. So here I have a
dictionary called squares. Where I have for two, I have four, for three, I have nine. For four, I
have 16. For five, I have 25. If I say dell using the keyword dell, squares, two. Okay, so this is
the key I'm giving, right? This is the key. So it'll go and find where is the key and remove this
whole key value pair. So what you're left with is three, four and five keys. Okay, so Dell is a
keyword here through which you can remove one key value pair by using the key value. Or
you can say squares. Clear. It will remove all the elements. And when you say print squares,
it just gives you an empty dictionary. Okay, so let's go down and understand. Okay. By the
way, there is a small difference here. So imagine if this was my dictionary. If I say dell
squares, it'll delete the whole squares. But now, if you try to print squares, it will give you a
name error, saying that name squares is not defined because you have deleted it, right? You
have not just deleted all the elements in the dictionary, but the whole dictionary itself you
have removed from memory. So it will give you a name error that squares variable is not
defined. Okay, let's go and understand some methods which are very, very specific to
dictionaries. The first method is dot copy. So when you say dot copy, you're going to copy
the elements. You're copy all of the key value pairs that you have. So from squares to my
dict. And if you print my dict, you get exactly the same value as you have squares. The other
very interesting thing, this is a slightly contrived example. This is a slightly complex way of
creating a new dictionary. Suppose, imagine I have, suppose I want to create a dictionary
like this. Math zero, English zero, hindi zero. Remember, the values here are the same. The
keys are changing. My K one, my k two and k three are changing. In this case, what I can do
here is I can say create a dictionary called subjects. Here I give an empty dictionary because
the function from keys is there for a dictionary. Okay? I create an empty dictionary here and
say dot from keys. And I'm saying these are the keys and the keys are there in a list. And for
all of these keys, the value is zero. So whenever I say this function from keys has a special
way of giving this value zero to all the elements in this list. So it gives maths equals to zero,
English zero, hindi zero. Okay, now let's go down and, oh, by the way, there is another way
of doing it, okay? So if I have a dictionary like this, and if I say subjects, subjects is the name
of my dictionary here. If I say subjects items, let's look at what it returns. It returns us a list
of tuples. Okay? It's returning us a list of tuples. Okay, this is one way of visualizing a
dictionary, okay? So subjects items will return me. Each of them is a tuple. And I'm returned
key value pairs as tuples. And this whole thing is returned as a list for me. Okay? The next
thing is, if I want to just find out only the keys. If I want only the keys. Okay, suppose I have a
dictionary, subjects with all of them. If I just say subjects keys, it will return me just the keys.
If I say subject values, it will just return me values. Okay. Similarly, if I create an empty
dictionary and if I say dar d, it gives me all the methods and attributes of dictionaries. So
these are all the functions. These are all the functions and attributes that I have of
dictionary. Okay, we have seen some of them, right? We have seen some of them. We have
seen pop. We have seen popitem. We have seen get. We have seen values. We have seen
some of them. We have seen keys here, right? We have seen some of them. There are others
also. Okay, next, just like our list comprehension, there is also a dictionary comprehension,
which is very exciting. Okay, so let's go into it. Suppose if I have a dictionary like this, I can
say pair in d dot items. So d items, right? D dot items will give us all the items in the
dictionary, right? When I say pair, pair for pair in, so I can use for p in d dot items, print p.
Then it will literally print tuples, where the first item in the tuple is the key and the second
value. And the second item in the tuple is the value. So if you want to iterate, if you want to
loop or iterate or iterate through elements in a dictionary, this is one way to do it. Again, the
in keyword can be used here, just like in list or anything. So when you say when pair in d
items, it returns you pairs of key value in a tuple and it returns you tuples here. Okay, so the
next thing, the next very interesting thing here is our comprehension strategy. Okay, so let's
assume I have a dictionary like this, a one b two, c if I want to create a new dictionary which
has key comma, sorry, key column value where the keys and values come from, d dot items
if the value is greater than two. Okay, so what happens now? It'll only pick so it will only
pick the key values in my new dictionary if the value is greater than two. So my new dict, if I
print, I get only c and d because for one and two, the values are less than or equal to two.
Okay, this is a very good example of list comp, sorry of. So this is a very good example. This
is a very good example of dictionary comprehension. There is another way of doing
dictionary comprehension. You can do more complex things. For example, if I have a
dictionary d, which is a one b two c, if I say that I want to create my key by concatenating
the existing key with the character c, and I want to create a value by multiplying each of the
values by two for key comma value in d dot items, if v is greater than two, which means first
it will go through each of them. So it will say this value is less than two, less than equal to
two, so it will not pick it. This value is less than equal to two, it will not pick. This value is
greater than two, this value is greater than two, this value is greater than two. Look at three,
four, five. Right? So now it will say, so you have c here, right? So how do I want to create the
new dictionary? I want to concatenate c to this. So it will create a key with Cc. And what is
the value? Multiply whatever value you have here by two. Right. So you get six here. What is
the second key? D. You concatenate c to it, you get dc, you multiply the value by two, you get
eight. Similarly EC and ten. Okay, again, dictionary comprehensions. Just like dictionary
comprehensions, DIct comprehensions are concise, but they can be confusing to people who
come from other languages. So I personally use them sometimes, but I avoid them when I
know that there might be people who do not know python very well, who might use it or
who might have to read this code. But it makes life very, very simple because you can write
very, very concise, elegant code with this.

You might also like