Now that you've seen how list comprehension works,
it's time to learn about another type of comprehension, dictionary
comprehensions.
This is also super useful because it allows us to create a new dictionary from
the values in a list or in a dictionary. For example,
we could write out our keywords like this.
We're going to create a new dictionary called new_dict,
and it's going to be created using this curly bracket now instead of square
brackets. And inside the comprehension, we get to set the new_key,
the new_value, and we get to loop through items in a list.
So any sort of iterable; a list, a range, a string, anything you want.
And this is the simplest form of dictionary comprehension.
Now remember that a dictionary comprehension is just a way of creating a
dictionary using this shortened syntax.
We could take this one step further.
We could also create a new dictionary based on the values in an existing
dictionary.
So we could take that dictionary and then get hold of all of the items inside
that dictionary, and then split it into a key and a value.
So now, we're looping through each of the keys and each of the values in all of
the items from the dictionary,
and we can use these key and value variables to create a new_key or a new_value.
And to go one step further for completion sake, we can, of course,
as always add our condition at the very end,
just like we did before with list comprehensions.
So let's see how this code works in practice.
I'm going to take my previous list of names
and then I'm going to click on this button to refresh my console so that all of
the previous variables get deleted.
So now I'm going to create a new list of names.
So I'm going to use the previous names I had before, and this is our list here.
Now, the next thing I'm going to do is I'm going to create a dictionary where I
generate a random score for each of these names.
So let's imagine we had a bunch of students and we wanted to create some random
scores for each of them so that they would get a random score between 1 and
100. Now for the teachers out there,
I really hope this is not what you do when you generate the students' scores.
But essentially we want to create a dictionary
which looks something like this. Each of the students in this list,
so let's start out with Alex. We want our dictionary to have a key,
which is the name of the student, and then a value,
which is their random score.
And we want to continue through that list of names until we've generated a
random score for each of them. That's the goal.
And now we have to use our dictionary comprehension.
Remember, when we're creating a new list using lists comprehension,
we're using square brackets. But now we're creating a dictionary
so we're going to use curly brackets. Now, inside these curly brackets,
I'm going to put down my usual list of keywords.
So it's going to be a new_key:
new_value that we're going to be adding to our new dictionary.
And then I'm going to do
a for item in list. In this case,
our list or our iterable is going to be all lists of names.
Now, even though in my keywords I always use lists,
it's just easier to imagine. But it's actually, in fact,
any sort of iterable like a range or a string or a tuple.
Once we've got our list of names,
we're going to name each of those names something,
and I'm just going to call it student.
So we're looping through all the students in our list of names here.
And for each of those students,
I'm going to create the student as the new key,
but the value is going to be a new, random number.
So in order to create a random number,
I'm going to have to first import the random module.
So I've cut out my previous code, imported random,
and let's paste that code back in.
So now we can replace the value for each of these dictionary entries with random
.randint,
and we can generate a random number between 1 and 100.
So now when I hit enter, you'll see our new students_scores
dictionary being created
and you can see we've got all of our students in our list of names, Alex, Beth,
Caroline, et cetera.
And each of them now have a new random score.
So it seems like everybody did pretty well other than Eleanor,
I guess. Random score generating seems to work. Now,
the next step,
we're going to take this a little bit further because this was quite simple.
We just did what we did before, which is looping through a list.
But now I want to loop through a dictionary. Luckily,
we've now created this brand, sparkling, new dictionary, our students_scores,
and I want to use that dictionary in my next dictionary comprehension.
So the next thing I want to do is to be able to create a dictionary called
passed_students. And this is going to be a dictionary
which looks through the dictionary of student_scores,
so imagine you have an entire high school where everybody's scores are logged
and you look at everybody who's got a score of 60 or over.
And then we're going to say, well,
those people have passed and everybody else has failed that year.
Essentially we should loop through this entire dictionary of items,
figure out which of these have a value that's equal to 60 or over 60,
and then we're gonna add those items back into this new dictionary
of passed_students.
So essentially, we'd probably end up with, um, Beth,
with Caroline, with Dave and with Freddy.
So it will still look pretty much the same as our students_scores dictionary.
So it would have something like Beth and then colon her score,
which is 72. But just the fact that she's included in this dictionary,
it means that she has passed.
And we will end up with this new dictionary where everybody is a passed student
and bear scores.
Have a think about how you might do this. And if you're up for the challenge,
try pausing the video and seeing if you can complete this.
As always, we know that we're going to create a new dictionary, so curly braces,
and then it's going to be our new_key: new_value.
And then it's going to be for, and now,
because we are looping through this dictionary and we want to get hold of each
of the values,
we're going to use the method that we saw in our previous slides.
So we're going to get hold of each of the keys and the value from our
dictionary. And then after the in keyword,
it's the thing that we actually want to loop through, which is our dictionary.
Once we've got our dictionary,
then we need to call the items method on that dictionary.
So it's items with a set of parentheses,
not just the items as an attribute.
So now that we've got our keywords down,
let's convert it to our particular case.
So our dictionary is called students_scores,
let's make sure I spell it correctly.
So students_scores.items gets all of the items in that dictionary.
And then for each of the keys
which is going to be students, and for each of the values
which is going to be their score, we're going to loop through this dictionary.
The new key is just going to be the student's name and the new value is just
going to be their score.
But what we need to do is we need to check using an if statement,
if the student that we're currently looping through has passed.
So the test is basically looking at the score and seeing if it is greater than
or equal to 60. And if that is the case,
then we're going to take this student as the new key,
their score as the new value, and place it inside this new dictionary.
So now if I go ahead and hit enter,
then you can see we've got our new dictionary being created,
which only contains all the students who have passed. So it's Beth, Caroline,
Dave, and Freddy, and we've lost Alex and Eleanor.
So in order to get more practice at this,
I've prepared some more coding exercises for you on dictionary comprehension.
So head over to the next lesson and we'll get started.