Welcome back.
I mentioned to you that a dictionaries of values can hold any sort of data type.
But what about the keys.
Up until now I've only used strings to denote a key but could I do something like this could I do one
two three like this.
Well let's have a look.
Let's give it a try.
If I do one two three and I click Run I get one two three.
Awesome.
That works.
What about what about true.
If I select if I have a key true and I click Run will that work yeah.
It looks like it works.
What about a list.
What if I have a list of let's say just a hundred.
Would that work if I click Run Nope.
It doesn't work.
It says on hash table type list.
What does that mean.
Dictionary keys need to have a special property a key needs to be immutable.
That is a key cannot change and numbers billions.
I mean this is a value that cannot change a string.
If you remember is a value that cannot change it's immutable but a list.
If you remember can be changed right on a list.
I can reassign let's say the index of zero to something else.
Right.
So because of that a dictionary says Hey my keys because I'm storing them in memory and I don't want
to lose them.
It has to be something that isn't going to change on me.
Maybe a programmer comes in and by mistake changes this array of 100 to have an index of something else.
Well dictionary doesn't really want that because it doesn't want to lose this value.
So a dictionary key always has to be immutable.
And as we learn a few other things like a couple that we'll see in upcoming videos you can use those
as keys as well.
However most of the time ninety five ninety nine percent of the time a key for a dictionary is usually
something descriptive like a string okay.
But what about this.
What if we have a nother string.
One two three.
And let's just remove this for now what happens when I search for one two three the string.
If I click Run I get.
Hello a key in a dictionary has to be unique because there can only be one key because that key is going
to represent a bookshelf in that memory space.
So anytime I do the same key and maybe add a value it's going to overwrite.
So this no longer exists.
A key has to be unique and it's something that can only exist.
Well just once.
Otherwise we overwrite it which is why you see hello here.
We've lost the array.
One two three let's explore this idea a little bit more in the next video.