Welcome back, everyone, to our continued discussion of Python basics in this lecture, we're talking
about dictionaries.
Dictionaries allow us to store data in what is known as a key value pair.
This idea of key value pair is actually going to show up a lot, especially when we begin to talk about
Django.
So keeping in mind a key value pair is essentially matching a unique key to some sort of value.
That way, when you want to actually look up the value, you just need to know its reference key and
notice how keys aren't typically going to be integers or numbers.
We tend to use dictionaries when it makes more sense to organize data lookup by a key rather than by
a particular position.
So even though the latest generations of Python will actually remember the order that items and key
value pairs are inserted into a dictionary, you shouldn't really be thinking about ordering or sequencing
too much when thinking about a dictionary and looking up things from a dictionary.
If sequence and ordering is important to you, think back to a list.
Key value pairs is kind of a different way to think about storing items.
So we're going to do is explore dictionaries with Python as well as their use cases.
Let's head to our code editor.
OK, here I am inside our editor.
Let's go ahead and show you how to make a dictionary.
So the way a dictionary works is you go ahead and set up your variable name.
Let's go ahead and call this dictionary employees.
Let's imagine we're trying to keep track of employees and what we're going to do here is just say something
like, let's say our chef.
Is Amy.
And then let's say we have another position like our CEO, and let's say that one is Jason.
OK, so I have employees here and this is now a dictionary again.
It's curly braces for the dictionary.
And then each key value item pair is separated by a comma.
This first string here is going to be the key, which then connects to the value Amy.
And keep in mind, it doesn't need to be technically strings on either of these keys are pretty much
always going to be strings, and you'll see why in a second.
It just makes a lot more sense to look things up like that.
But the actual values themselves could also just be the numbers are integers or even other dictionaries,
other lists.
For example, I could say for employees, we could say like number or something like that and put in
45 or something like that.
So just keep that in mind.
Doesn't necessarily need to be strings.
But what I want to show you is how you would actually look up information from a dictionary.
Now, since I have employees as a dictionary, I want to be thinking and things of key value pairs.
So what does employees actually represent?
Employees represents a key value pairing of all the positions across the company.
And then who is in that position?
So what I can think of is, OK, I'm interested in who is the CEO of my company.
So now when I look it up instead of previously or looking things up, I index position by passing the
zero or one, etc. I'm looking things up by their key, so I would look up CEO and let's go ahead and
print that result so I can actually see it.
So we're going to say print employees see you.
You'll notice it still uses the same square bracket notation as indexing did.
But what you actually pass in is going to be the key.
So I'm going to save that.
Let's run example that PI and now I can see Jason's being reported back.
So notice how useful this is.
I now have almost like this Rolodex object, or I just need to remember that indexing key.
And then I can get back the relevant value.
And what's also really nice is that I can be able to keep these keys.
But then if somebody changes, for example, let's say we hire a new position at the company, I can
go ahead and easily add that in.
So again, it's key value pairs for the dictionaries.
And now let me show you how to add in a new employee.
So let's imagine we just created a new position, which means I need to add in a new key to my dictionary.
How do I do that?
Well, we call your dictionary.
In this case, we called our dictionary employees.
And then what we're going to do here is in square brackets, you refer to the new key you want to add
in almost as if it already existed in the dictionary.
So let's imagine we just hired a waiter at our company because maybe we have a restaurant style company.
So I say, OK, we just hired a waiter.
And now I'm going to set that waiter equal to.
And let's say I hired Mike to be the waiter.
So now I can go ahead and print employees, I'm going to print out that whole dictionary.
Let's go ahead and save that.
And when I run Python example that I notice that I was able to add in a waiter and mike.
So if I ever want to know who the waiter is, I could just look up employees and then pass in the position
I was interested in, like waiter.
And I can see, OK.
It was, Mike, that way in the future, if something gets changed or updated, I can go ahead and easily
figure out, OK, who is it now in that position?
So I have employees and then I know how to add in a new key with a new value.
So this is adding in a new key value pair.
Now, what if I want to update a new key value pair?
Maybe we got a new chef, how do we actually take care of that?
Well, what I can do.
Is the following if I want to perform an update to an existing key value pair, what I have to do is
I call again employees and then I grab the key I want to update, for example, maybe I want to update
the chef.
And let's say I'm not a chef's.
I'm going to update this to be Jose.
Which means I'm going to print out employees and let's go ahead and print out the chef.
And I'm actually going to print out chef and both so we can see that, in fact, it was updated.
So I'm first going to print out employee chef with the original dictionary here, and then I'll go ahead
and print out.
Update.
Chef and then I will update chef here.
Kind of similar to the syntax of adding in a new key value pair.
And then we're going to print this out, so the output I should see will be Amy Update Chef Jose.
So let's go ahead and explore that and go ahead and kind of pauses VIDEO If you need to take a look
at the syntax there.
But let's run this and I see Amy update chef and then Jose.
All right.
So now we understand that I have the ability to add in new key value pairs, as well as update existing
key value pairs.
The other thing I want to point out here is remember, I'm actually getting back an object here of what
that value was.
In this case, it was the string Jose, which means I can actually perform actions on that object so
I could print employees.
Chef, but employee chef, if I just have, this code is going to be Amy.
So let's see that right now.
This will be Amy, which is a string object, which means I can actually call things like up here on
this open and close parentheses.
And it will work just fine.
So the upper is being called on the actual string that's returned with employees chef, which means
I now get back Amy in all caps.
So I just want you to keep that in mind that as you begin to store different types of objects in a dictionary,
you're going be able to use those objects or even assign those objects to a variable and then call methods
on them.
So let's actually show you another example.
Let's say we are keeping track of different stock price histories, so I can say stock prices.
And I'm going to create a dictionary here.
Let's go ahead and create, let's say, the stock price for Google.
And now this value is actually going to be a history of prices, which I'm going to represent as they
list history of prices makes sense to keep as the list because I want to make sure that actually retains
order.
I don't want to accidentally have some sort of changing of the order because it's a history.
It's things that probably shouldn't change order price on day one, price on day to price on day three
and so on.
So I'm going to make up some prices here.
Let's say it's two hundred on the first day, 210 on the second day and then 220 on third day.
Then I can put comma and add in another stock.
So let's go ahead and say maybe Jimmy as a stock and it's going to be.
This is just a stock ticker.
Maybe it's 20 dollars on one day, then it just $100 on another day, maybe $300, etc. So this is a
nice combination of a use for a dictionary and a use for a list.
A dictionary allows me to quickly call something like stock prices, and this put in the ticker symbol
that I'm interested in, like Google.
And then this will actually be a list of the history of stock prices for that.
So then I could just print this out.
So let's go ahead and say print.
Stock prices, Google run that example, and I could see 200 to 10 to 20.
What I could also do is actually assigned this to something say like history is equal to stock prices
of Google.
And then maybe I only want to print out the first day of this actual stock price.
How could I do that?
I could use a bit of string literal formatting here and say first day price is.
And then pass in history here, if I just pass on history by itself, they would actually print out
this entire list if I only want the first day.
I would just get history and index it at zero.
Not that I'm actually putting in the indexing in the print format statement.
So here I should see first day price is two hundred and know how I'm starting to combine different ideas.
I'm starting to combine the idea of a list with a dictionary and then starting to combine that idea
with a lookup and a variable assignment, then combining that with the idea of indexing and a string
literal formatting.
And hopefully you can begin to see that, you know, it's just these three lines of code.
I have something with some logic, some sort of just common sense functionality of, OK, look up stock
prices.
I get the history and I can report that back to the user, which is honestly how most stock websites
are going to work.
They have some sort of way of storing the data.
They have some sort of way of looking it up and then some sort of way of reporting it back to you.
Now, the last thing I want to show you are just a couple of different ideas of nested dictionaries.
Dictionaries are actually going to be pretty important when we first start learning Django.
It's the format that Django uses to send information back to the web page.
And the fact is you can actually nest dictionaries.
This is not super common, but I do want to show you the fact that you can do it.
So I'm going to say my dictionary here is equal to and I'm going to create a dictionary that contains
another dictionary.
So we're going to have a key here called outer colon.
And then I'm going to put another set of curly braces here and then put a key called inner.
And then let's just give this a value of 100.
OK, so note that I have a dictionary within a dictionary.
This is totally valid in Python.
And the reason I'm pointing it out here is because there may be some times where we actually do this
within Django and we're going to do here is I'm first going to print out my dictionary and then the
key is going to be outor.
So what do we actually expect to be returned here if I call outor?
Well, just run this and I see I get back that dictionary, which means I could technically, if I wanted
just one hundred, I could say my dictionary outer and then have another set of key look up and say
returned back in her.
Save that change.
Go ahead and run this, and now I just get back the number one 100 so I can see some logic here of the
outer key and then from this dictionary, grab the inner key value.
OK, finally, a couple of methods that are important for dictionaries.
Let's just quickly create a dictionary here.
I want to create a dictionary with key one.
This is just going to be a value of one hundred comma, let's say, key to a value of two.
And then let's say key three and a value of four hundred.
And now what I'm going to do is show you how to print just the keys.
If you just want the keys, you can say my dictionary keys open close parentheses there.
And if you run that, it'll just report back almost like a list of keys.
It's technically a kind of a specialized object here.
Dictionary keys, but can think of it as just a list, a key one key to key three.
If you want just the values that has the numbers 100, 200 400, then you just call values.
So let's check out that change.
And here we just see the values 100, 200, 400 notice now, I don't actually know from either of these
outputs.
What are the key value pairs if you want those key value pairs?
What you do is you call items and that's actually going to display these key value pairs for you.
And you'll notice there's a use of parentheses here, and these are temples or temples.
We haven't actually learned about tuples yet, so we're going to cover that in the next lecture.
But I want you to just see it right now and discover, Oh, there is this other way of returning back
this key value pairs in this tuple or tuple formation.
OK, so that's it for the lecture on dictionaries.
The main takeaway is to understand that there's key value pairs, and you can look them up using the
indexing notation like this pass in.
What's the actual string?
He won.
Save that and then run it, and then if you ever want to add a new item, you just call that dictionary.
And if you want to update, you call an existing key like this and then do some sort of reassignment.
Or if it's a totally brand new key, it's essentially the same syntax.
You just specify the brand new key with some sort of default assignment.
All right.
That's it for this lecture.
I'll see it the next one.