So we've talked a little bit about conceptually what a dictionary is in Python.
In this video, we're going to look at how we actually define them.
So you are going to see how we can put a dictionary into our program,
right? Let's check it out.
Here I have an example program that just creates
a bunch of dictionaries and then prints them out so you can see what they look like.
The first thing that we have to understand is that in Python,
you use curly braces to indicate that you want a dictionary.
So we can see here,
to create an empty dictionary,
I just have an open curly brace and a closed curly brace,
and that creates a dictionary with no keys or values in it.
All right, let's see what that looks like when we print it out.
Like a lot of things in Python, when we print it out,
it looks exactly the same as when we wrote it into our program.
So, open and closed curly brace is an empty dictionary.
When we print that out, we get open and closed curly brace. All right.
Well, it's not very interesting to only have dictionaries
that have no keys and values in them,
so let's look here at a couple of dictionaries that have numbers as keys and values.
So first we have this simple dictionary that has one key-value pair.
Now notice after the open curly brace,
we now have a number,
one, followed by a colon,
and then the number two and then the closed curly brace.
So, one : two means take the key one and map it to the value two, right?
So that's one key-value pair inside of our dictionary.
Now, let's look at squares.
We can have more than one key-value pair.
In the squares dictionary,
we actually have four.
The keys are one, two, three,
and four and you can see each key is followed by a colon and then a value.
So the key two here maps to the value four.
And notice that the key-value pairs are separated by
commas to indicate that we're moving to the next key value pair.
Let's print these out and see what they look like.
And yet again, they look basically
exactly the same as when I wrote them in my Python program.
Now, our keys and values are not limited to be numbers.
As we know, the keys can be anything as long as it's immutable,
and the values could be any Python object.
So, let's create a dictionary cipher where we
map characters to other characters, all right?
So I have p mapping to o here,
and y mapping to h and so on.
Let's print that out and see what that looks like.
Yet again, it looks pretty similar to how it looked when I wrote it into my program.
And we have the different characters mapping to other characters here.
So p maps to o,
just like we would expect from what I wrote in the program.
Now, the keys and values don't have to have the same type as each other either.
Let's create a dictionary of good instructors.
This should take the name of an instructor and map it to
a Boolean value indicating whether that person is or is not a good instructor.
I've written some things here,
you can see if you agree,
and we can take that further.
It doesn't just map to things like Booleans.
Let's create a dictionary that maps countries to a list of their two largest cities.
So we have a couple of countries here.
You might notice here,
I've got Texas in there. We're pretty big.
We believe we're our own country,
and we're just as good as every other country, okay?
All right, let's print these out. And if you look at it and see what's going on here,
you can see once again we've mapped
keys to values where the values are different types than the keys.
So all these dictionaries so far I've written directly
a dictionary literal where I've used the open and closed curly braces.
We can also write dictionaries a little bit different.
There is a dict function.
The dict function creates a dictionary as well.
So if I want to create an empty dictionary,
here I'm calling it empty2,
I can just call the dict function with no arguments,
and let's see what happens there.
I get an empty dictionary, okay?
So we've seen the literal syntax of the open curly brace,
closed curly brace is an empty dictionary.
I got that again just by calling the dict function.
What if I don't want an empty dictionary?
How can I call dict to make that happen? All right.
Well, dict takes a list of pairs basically.
So I created here a list of tuples and called it data that has
the first tuple is the number one and the string "one," the word one,
and the second tuple is the number two,
and the string "two," and so on.
And so when I called dict with this list,
it interprets each of the tuples in that list as a key-value pair.
So, it's easiest to just see that in action.
Let's look at it. The keys are now the first elements of the tuples,
one, two, and three,
and the values are the second elements of the tuples So,
the number one maps to the string "one."
And you might use this dictionary to convert from numbers to words for instance.
Finally, you don't have to pass a list,
you could actually pass another dictionary.
So if you recall,
If I go up here,
we have a dictionary that we called cipher that we created earlier.
Let's now call dict with cipher as an argument to create cipher2,
and print cipher2 out and see what happens.
Well, we get a dictionary that looks exactly the same as cipher.
So we took one dictionary, copied it over,
and created another dictionary.
So now you've seen how you can create dictionaries in your Python program.
In fact, we saw two different ways in which you can do it.
You can write a dictionary literal directly into your program,
and we know that that means that you use curly braces around the dictionary elements,
and then each element in the dictionary has a key-value pair.
We have a key, then a colon, then a value.
And if you have more than one,
they're separated by commas.
That's one way to create a dictionary.
Another way is to use the dict function.
And if you pass it nothing,
you get an empty dictionary,
and then you can pass it either a sequence or another dictionary.
If you pass in another dictionary,
it makes a copy of that dictionary.
If you pass a sequence,
each of the elements of that sequence has to also be a two-element sequence.
It interprets those two-element sequences as
key-value pairs and creates a dictionary out of them, right?
So now we can create some dictionaries in our program.
It might be nice to be able to use them too, though.