-: Hi guys, and welcome back.
In this video we are going to talk about dictionaries.
Dictionaries are another structure in Python,
like lists, sets, and doubles,
that allow us to interact with data in a certain way.
And what dictionaries are for
is to associate keys and values together
so that if you know the key
you can access the value very easily.
For example, you could have a dictionary
of your friend names being the keys
and their ages being the values.
That way if you knew the name of your friend,
you can very easily get the value out, their age.
Let's get started.
Let's say we have a dictionary friend ages
and it's equal to the curly braces,
and inside it you've got your friend name, Rolf,
and then a colon and then something like 24.
This is a dictionary of a single element
where the element is this key value pair.
The pairing is done with the colon
and that means that 24 is a value
associated with they key, Rolf, which is a string.
Keys must be strings, they can also be integers
and any other hashable types.
Now I know we haven't talked about hashable types yet,
but for now just think of them as integers and strings.
If we want multiple values in a single dictionary,
you can separate the key value pairs with a comma.
So we can have Adam, 30, and Anne, 27, for example.
And here we have a dictionary with three values, three keys.
Rolf, Adam, and Anne.
And each of those keys has a value associated with them.
Rolf has 24, Adam has 30, and Anne has 27.
If you wanted to access one particular element,
you can do friend underscore ages
and then using subscript notation,
access the appropriate key.
So in here we would put, for example, Adam
and then we would access the Adam key
inside the friend ages dictionary.
Remember that in lists and doubles
you could use subscript notation
to access a particular index such as zero, one, and two,
but that won't work in dictionaries.
You must access the keys themselves.
Of course, if we printed this out,
you'll see that we get 30 out, just there.
If you wanted to change a particular element or add one,
you can do that by doing friend ages,
and then for example Bob equal 20.
Then if you print friend ages,
you'll see that Bob will have been added to the dictionary.
So when we print it out, we've got Rolf, Adam, Anne, and Bob
And if you wanna change one, just do the same thing,
but with an existing key.
And now Rolf is 20.
So that is how you can add a key,
just access it and set a value to it,
or change an existing key.
Usually when we are working with dictionaries that are used
to represent multiple things in the real world,
such as multiple friends or things like that,
it's quite convenient to have a list of dictionaries.
For example, let's make a list of our friends.
Now in the past, we've had a list of friends,
such as Rolf, Adam, and Anne like this,
but you can actually have a list of dictionaries,
each dictionary describing a friend.
By the way,
I'm not sure if we've looked at this split syntax for lists,
but lists don't care about white space,
so you can put multiple values in one line,
or you can split them across several lines,
however you like.
Going back to our dictionary, you can, instead of Rolf,
create a dictionary that contains name
and Rolf associated with it,
and age and 24 associated with it.
Now if we do this for each of our friends,
and I will do that with Adam and Anne,
and their appropriate ages,
That means that now what we've got
is a list of dictionaries
and each dictionary represent a friend.
But that means that we can store more
than just their name in each element.
Before we were constrained
to just storing their name in our list,
but now we can store more data if we want.
If you print friends out, you'll see that you get back
your list of friends there.
And of course if you wanted to print a particular friend,
you would need to use the index, because this is a list.
The friends list needs to be accessed by index
so if you wanted to access index zero,
that would give you back this dictionary.
Index one would give you this dictionary.
And index two would give you this dictionary.
So let's say we want to access index one,
which is this dictionary,
if you print this out
you'll see that what you get back is,
naturally, the entire dictionary.
That means that now, after friends one,
you can put another subscript notation to access the name,
for example.
And doing this would give you Adam.
So this is how you can access a list of dictionaries,
or a particular value inside a list of dictionaries.
If you had a list of lists,
you would access multiple indices in succession like this.
And if you had a dictionary of dictionaries,
then you would access multiple keys in succession.
Let me say of course that you can nest these lists
and doubles and dictionaries however way you want in Python.
Let's take a look at another example
where we have a dictionary that maps the student names
to the percentage of attendance they have to their lessons.
So Rolf has attended 96% of his lessons.
Bob 80% and Anne 100%.
You can iterate over a dictionary, using a for loop.
And what this does is it gives you the keys.
So if you print this student variable
that we're using here for iteration
what you get back are the student names.
The keys in your dictionary.
Using that key, you can print something like this.
Which will print out the student variable
which is the students name
and then you can access the student key
in the student attendance dictionary
using subscript notation to get their attendance.
So if we save this and run it
you now see that we get Rolf 96, Bob 80, Anne 100.
You can of course put a percentage sign there if you like
to make it a little bit nicer.
However accessing a dictionary like this in a for loop
doesn't look the best because there are other, better ways.
The better way is if you do student attendance dot items
and then here you can do for student comma attendance
in student attendance dot items.
And then here you no longer need to access dictionary
because you've got it in the attendance variable.
So what we've got here is that student attendance dot items
gives you two values for each student
that you can get as two separate variables here in Python.
Saving and running this
will give you exactly the same as before.
You can use the in keyword in a dictionary
to check whether a value is one of the keys
of the dictionary.
So for example, if Bob in student attendance,
you can print something like
Bob attended this many lectures.
Otherwise, you can print that
Bob is not a student in this class.
So, the in keyword allows you to check
whether a value is one of the keys.
Notice that you can't check whether one of the values
is in the dictionary, you can only check the keys.
Finally, you can also get just the values back if you like.
If you wanted 96, 80, and 100
without worrying about the keys,
for example if wanted to calculate an average,
you could have a variable called attendance values
and make it equal to student attendance dot values.
And then you can print the sum of attendance values
divided by the length of attendance values.
And what this is going to give you is
add 96, 80, and 100 together and divide by three
which is the length of what you get back which is a list.
So if you run that you get back 92.0.
Going back to what we had earlier,
which is this syntax where you have two variables
being defined, separated by a comma inside a for loop,
we're going to learn more about that in the next video.
So if that's unclear, don't worry about it,
we'll look at it just now.
I'll see you in the next video.