Hey everybody.
Welcome back to the coding Party.
Mark Price here at death's slopes dot com.
And today we're going to talk about dictionaries and what I'd like you to do before we start is actually
do me a favor and do a google search type in how do hash tables work in computer science how to hash
tables work in computer science and go read some articles and start getting an idea of what a hash table
is and then posits video and go do that and come back here like in 15 minutes because I want you to
have a base foundation.
Did you do that.
I'm sure you didn't because now you listen to me.
Until you start getting those job interviews and then they're like how does this work in you're like
Mark you're right this whole time I should have been listening to what I just said and read about this
and this and that and that.
But instead I just was hoping that you had a video on it and to whole.
But anyway I went to read it and I got the job.
That's always happens.
Don't listen to me.
Listen to me whatever.
Do whatever you want your life.
All right.
So I'm creating a new playground here and we're going to call this one dictionary.
Dictionaries so in swift a hash table it's called the dictionary and in different languages the hash
tables are hash maps or in this case dictionary here.
And I'm going to go ahead and store this in my dictionaries folder.
OK.
There's our playground and I'm a pull up in this nice little info graphic that Apple has created.
So there are three major collection types in swift.
We have the array OK which is which is in an ordered set.
Excuse me an ordered list of items and you can have multiple values of the same type in an array.
So for instance index 0 can have let's say the word 6 eggs but also index 5 can have the exact same
word six makes doesn't matter.
OK a linear list ordered items.
OK.
Now we've got something called the set.
Now a set is has items that should only have one type of value meaning the values need to be unique
but they're not ordered.
OK but they are unique.
And then we've got this cool thing here called the dictionary.
Now you're going to use dictionaries and arrays more than you are sets.
When you're working with swift The dictionary has keys and values and we're going to have an example
similar to this today basically where the key is of a certain type.
And then there is a value that's related to that key so that the values can all be the values can be
the same as the other values of the other keys but the keys must always be unique.
OK.
And these this is not ordered.
OK.
And there are actually very complex and efficient algorithms algorithms working under the hood that
help you retrieve and fetch items.
Imagine if you had a dictionary that had a million keys in it.
You know it's not going to go through the entire list of keys.
OK it's going to start breaking things down and segregating out sections until it finds the key and
the fastest route.
Very complex things going underneath the hood.
Again if you do that search I told you too on how the hash tables work.
There's some amazing YouTube videos on how this actually works underneath the hood if you're into that
kind of thing.
And anyway so we're going to be working with the dictionary today and we're going to work with keys
and values.
OK.
So this is straight out of the Swift books with three e-book.
Very important that you read that book.
Of course I've said that before.
And here we go.
We're going to create our first dictionary names of us and I want I want you to do names of integers.
Let's say we want a dictionary that lets us give a word a visual word that you can print out for a number
type meaning like let's say I want the number three to say the word Three things like that.
So what I can do is I can say names of integers equal or subscript here the square brace here and we're
going to say three equals three.
Now I know what you're thinking.
Oh this is just like an array.
This is like the third index right.
No it is not.
This is actually the name of the key I just made the names of the keys of type int.
OK this has nothing to do actually with an array it's not a subscript in that.
You're grabbing an ordered amount of items.
OK.
This is just happened to be what I type I chose for the key.
This could also have been a string here which is interesting.
So this is a simple example of your very first dictionary.
And what we're doing here is we're just creating one on the fly.
Initializing it using type inference.
OK.
We're creating a dictionary with a key of type and and a value of type string.
I would not be able to do something like this and put a 3 in here.
OK.
Because the value needs to be a string cannot assign value of type it to string cast of keys and values.
All of this may look familiar to you if you've worked with the race names of integers or see one more.
And let's say 44 equals forty four.
OK.
The idea behind this dictionary is there's a unique key and then there's a value a word value for that
we're just explaining in words so simple example.
If you want to click quickly clear out a dictionary empty all the items out of it.
It's as easy as doing this.
OK.
Square braces colon square braces it's going to clean out all of your keys and all of your values.
Nice and new.
Let's do a more real world example let's say we have an app.
OK.
And let's talk about airports of the world we want to have an app has all the airports of the world
well every airport can only have one unique airport code.
Imagine if airports had the same airport codes it would be crazy like imagine if Ireland had an LAX
Los Angeles had an L.A. X you know and Dubai hadn't lax it wouldn't work and it would mess things up
so luckily we have unique airport codes that we can use throughout the world and each one has its own
name its own unique name.
And you know what maybe the airport codes are different and maybe two airports can have the exact same
name but that wouldn't matter because it has its unique code and that's the most important thing.
But we still need the name.
So that's the advantage of using dictionaries is the Keys have to be unique.
They have to be let's do that now to create our dictionary sort of our airports.
And this is going to be of type string so we're going to do it in explicitly declared dictionary.
So the key is of type string and the value is of type string.
Now let's go ahead and initialize it here right off the bat.
So we're going to create some dictionary literals here that we're initialising right when we declare
as well.
So Toronto
can.
So we've just created a dictionary that comes preloaded with one element in it or one key value combination.
But we want to do more so we're going to put a comma here and L.A..
OK.
And what we're going to do is say loss or Los Angeles.
OK.
Pretty cool actually.
Los Angeles say international.
OK.
So I actually did not go there.
There we go.
OK.
So what is happening here.
So we put in the key name first and then a colon.
OK.
The colon separates the keys and the values of the key name goes first the call and then the value the
comma separates the elements or the combinations.
So this is one key value combination.
This is a second and it's separated by a comma.
So key name colon value comma key named colon value comma key name call and value comma.
And we didn't have to explicitly declare it like so we could have used type inference and it should
just work out of the box for us indicating that yes this is a string key and this is a string value.
Pretty cool.
So now we've got our dictionary full of airports and there's some of the other things we can do with
this.
We can print the Air Force dictionary has.
Let's you some string interpellation airports rockhound airport or Osei element's items.
OK let's make this a little bigger here so you can see this area.
Put the quotation to soon.
So the airport's dictionary has two items.
Is that true.
Well yes it is one and two guys so you can get the count of a dictionary.
And what else can we do.
If airports is empty you can see if it's empty or not.
To Trent the airport's dictionary is empty.
OK.
So you can check if it's empty.
The simple little came in and you're probably wondering well how do I add new items to a dictionary.
Well since we're not making an ordered array where an element goes onto the back of the list we're inserting
at a certain place we don't care about that.
All we care about is the unique key.
So we can just say airports let's add a new one.
LHR equals London.
So what we're doing here is we're setting LHR.
This is the key name OK we're setting that unique key key value to be of the value of the type not type
but the value to be London.
OK.
So the unique key LHR is going to be equal to London.
Okay.
And we just added a new element at this point in time.
Okay well what if we want to update this or overwrite it it's just as simple as doing the exact same
thing.
Okay.
So what we did here was whatever was in that spot we have now overwritten it's gone.
So that's how you replace elements.
That's also how you update elements didn't matter if one of these key names existed before or not.
No it does not matter because it will be created the moment you assign it there.
OK.
So if you're thinking well if there's nothing in there and I tried to assign something to it it's going
to give me an air.
That's not the case.
Unlike in an array where if you try and grab an item that doesn't exist.
Ok you get the crash you know.
It's different here.
OK.
What else let's say airport loads up here.
All right.
Say airports.
Create a new one dev equals slopes International.
And you guys know that deps slopes has its own airport now.
And we have Wi-Fi and roller coasters and birthday cakes.
Really cool.
And then we realized it was a dream and it doesn't exist so how do we actually remove an item completely
we want to go on key and everything because there is no there Loeb's airport with cakes and Wi-Fi and
roller coasters would be cool.
We probably had a draft in there too.
But my dreams are now crushed.
So airports dev.
Neal.
You can set it to nil which will completely destroy and remove the item.
It is gone.
So I hope you're starting to see oh I can work with keys and values they're unique.
I can do different things with them.
You're going to use these a lot.
Hey we're just scratching the surface here.
You can also iterate through through an a dictionary which is cool as well.
You can get all the keys and values out of it which we'll do right now.
So House today here for each airport code an airport name in airports.
OK.
So what we're doing here is a four inch loop.
OK.
And the for loop in this case in the dictionary it's giving us what's called a tuple.
OK a tuple is a data type or a data construct that has one or more elements in it.
OK so it's returning as a tuple you don't have to remember that just know how to use this.
OK.
And what it's going to do is it's going to pass in the key name and the value could also have said key
and Val.
OK.
Same exact thing.
So it's going to give us the key name and the value and it's going to go through the entire dictionary
on each one of those.
And there is an algorithm that goes underneath the hood that's efficient and grabbing this information
although not as efficient as other ways of accessing information.
In most cases in many cases you don't want to iterate through an entire list of dictionaries if you
want to iterate through a list of something you know maybe an array is better data structure.
You can definitely go through here and I have before so I'll say airport code.
All right.
And then we'll put up the last colon here and then we're going to say airport name.
OK.
So we're just going to go through and print the code in the name or the key and the value.
And here it is.
There is the key there is the value.
There's the key there is the value key and value OK.
Looking good that is good.
All right.
So there's our airport to airport name perfect.
What else can we do.
We can also just do the keys and values individually so for each airport code or we can even just say
for each key an air port Stuckey's so you can print up the keys right.
We can pass in the key
pretty cool and we can also do the same thing with the values for each vowel in airports values values
no valid just a variable that I've created a constant.
You
and organised pass and ballet here.
OK so value value value so you can work with kids and values.
The kids are unique.
Mostly most of the time you're going to work with strings here and you're in your data types you can
play around with other things.
But one of the big uses of dictionaries is when you're working with Jason data that you're getting from
the Internet it's going to come down in a bundle just on file and you're going to grab things by keys
and values because Jason is very much similar to the notion of of dictionary keys and values.
And if you don't know what I'm talking about let's just take a look at it right here.
We can go to a Star Wars API that's this free API this one is created on the Internet.
It has lots of information on it.
And so right here it's pulling up some Jaison this one.
Luke Skywalker.
And so this is a dictionary where the keys are of type string.
So here's a key called name of type string.
So name the string Luke Skywalker the value type of this one is a string.
Let's find one that's not a string here.
These are all strings on here.
Let's look at some of their other API features to show you how it's going to kind of translate in your
code.
Israel strings as well.
They could also been numbers.
This this company or this person chose to put all the values in as strings instead of numbers so that's
really important to to understand is when you download information from the internet or Jason you have
to abide by whatever they have created for you.
So whereas I may have put this as a number in integer they chose to use a string for both the key and
the value.
There's another API that I like to work with as well.
Hoquiam on our active pokie API OK another one that somebody put up which is kind of cool.
And here's one perfect example so.
So this is a dictionary object right.
And the keys are all strings.
But in this case the key is.
So the key ID has a value that's a number it's an integer and base experience is an integer.
Height is an integer is default is a boolean.
So you can have a boolean value type.
So this is this is interesting to look at this one on this one.
The key name is a string but the value is an array.
That's really interesting.
Can we do that with swift.
Like let's say we wanted to create abilities.
That's really interesting.
Let's give it a shot or it's var let's say abilities.
Me a dictionary right.
So the dictionary or the bill t's dictionary is going to be a dictionary of type string.
And then what we're going to do is we're going to say an array and the array is going to be an array
of what can be an array of dictionaries right.
So let's see what it does here of type string and string.
I'm just curious where the syntax is wrong here.
But anyway my point here is you can have.
You can have different items inside of your dictionaries.
OK.
Such as a boolean in OK you can do all kinds of different things with your dictionary so it doesn't
have to just be strings and integers.
So lots of cool stuff.
This is just scratching the surface here.
OK.
Just touching the surface if you want to get a better synopsis of how dictionaries work again do that
Google search I told you about how to hash tables work in computer science you can also go look at Jaison
and start getting a feel.
And I said based on that.
J.S. Oh and get a feel of how things work with with weather and how it might translate to your.
And that's it.
Lots of good information.
So good old man.
And let's move on and forward price down slopes dotcom at CNN.