Hey, hello! Let's look at dictionaries in this lecture. This far we've looked at strings
we've looked at lists and tuples and now we have yet another data type and these are
dictionaries. Let me open the python shell. And let me create a list first I'm just
creating this for reference, so to compare lists with dictionaries, but you
don't need to create a list for creating a dictionary. So to create a dictionary
you need curly brackets, so open and closing curly brackets and then inside
that you'd pass the items of the dictionary. Now the difference here with
dictionaries is that instead of letting Python add default indexes for your
items you want to create those indexes yourself exclusively so therefore for
dictionaries you do something like this. Name, a column and then the item. Let me
write another one, surname column and then the other item and yeah that's it.
That's the dictionary. So what we have here is when I was talking about
indexing I was referring to these things. Actually these two are referred to as
keys, not indexes so for dictionaries you can create custom indexes so to say and
so these are keys so we have key and value, key and value. This is good because
now if you want to access one of the values of the dictionary again you'd use
square brackets just as you did with strings lists and tuples and then there you
pass the key which value you want to extract, so if you want to extract John,
you want to pass the key of John and that will extract the John value. Something
to note is that dictionaries are an unordered colecton
of items and so sometimes you may notice that the order of the key and
values is not maintained so you may see that surname has gone to the first place, so
surname Smith and the pair has gone the first one and name John is the second
one and so on. So this is different from lists, strings and tuples. The reason why
Python doesn't keep the order is that you know the way you access dictionaries
is you exclusively pass the key of the dictionary when you access it and so you
don't need to know the order of the dictionary. And whenever you want to access
something you say the surname for instance and that will the return Smith
no matter where Smith is, so whether it is the first one, the second one or over
and so on. And interestingly in dictionaries you can also pass, you can
have like lists as values or even tuples, so let me write a circle there or let me
keep it like that and let me add another key and pair here. Let's say cities. Let's say
Joe Smith lives in three cities, Porto and San Diego and Bali. You close
bracket there so as you see we have the key here, we have a column and then you
have the value which can be anything so it can be string, it can be a number, it
can be at list, a tuple. In this case that's a tuple so if you access now
cities like that, you'd get… Sorry, I'll add two bracket there, so you'd get the tuple.
And then you can also do tricks like you know if you want to access Bali you'd
have other brackets there and pass 2. And that would return Bali, so why is that?
That's because you know up to here you have a tuple,
but that's a triple object and then from that tuple you use again the
square brackets notation to access an item from that tuple, so you want item
with index two and that is Bali and yeah that's about dictionaries. Try them out
some more, play around and you will be able to comfortably create and access items
from a dictionary. So I hope you enjoyed this and talk to you later.