Let's take a look at a dictionary comprehension.
So a dictionary comprehension is a lot like a list comprehension,
but instead of having a for loop that could be 3, 4, 10
lines of code, we have it basically set up in one line of
code. So let's go through the long way, and then let's go
through the short way, which is a dictionary comprehension.
So as an example, let's say we have a list of 'keys', and we've
got a 'name', and 'age', and a 'role';
and then we've got a list of 'values' in here as well.
And I'm going to put in just my information here, and I am a 'Teacher'.
So what I could do, is I could create an empty dictionary called 'person',
and I could say 'for key, value in zip(keys, values)',
Remember, 'zip' is going to take these two lists and merge
them together in a paired tuple.
So it's going to be - 'name' with 'Kalob'; 'age' - 30; 'role' as 'Teacher'.
And then I could say, 'person[key] = value',
and when we print this out, we have a dictionary 'person' with
the key of 'name' is 'Kalob'; key of 'age' - 30; key of 'role' is 'Teacher'.
And let's just verify this is in fact a dictionary.
We can see that it is by how its syntax looks, just like this.
But just to be fair, we know that it's a dictionary.
Now, this is a very small example,
but what we could do instead is put this all into one line.
Well, not all of it.
We can't put this into one line.
Those need to be pre-existing because they're variables we're going
to use inside of the 'zip' function.
So what we could do here, is let's say 'person2' so that
we don't overwrite the original 'person' here.
'person2 = {',
and what I'm going to do is actually put this on a new line,
just because I like to put this on a new line sometimes to
make it a little more readable.
And then I'll put it on a single line
if it's short enough. If your list comprehensions or your
dictionary comprehensions ever get too long, feel free to
put them on multiple lines, that's okay.
And so what we want to do here is write our for loop.
So 'for key, value in zip(keys, values)',
Now, in a list comprehension, we would really just say 'key',
'key for key, value in zip',
and this is what it's returning.
But a dictionary has a 'key' and a 'value' in it, and you can
actually see that it's got a little cool in here.
So this is your 'key', this is your 'value'.
We do the same thing in a dictionary comprehension.
'key: value'.
And let's go ahead and just put this back all on one line.
And let's run this, 'person2', it's the exact same thing as what
we wrote up here.
Now we could even add 'if' statements as well.
So let's go ahead and create 'person3',
but let's say we didn't want the 'role' in there.
We could say 'if', at the very end of this, the 'key ==
age', we could get just the 'age', or we could do the inverse
and we could say, only ever do this if the 'key != age'.
So it's just going to return 'name' is 'Kalob', and 'role' is 'Teacher'.
We're going to jam that into person number three,
[no audio]
and 'person3'. Bam.
We have now just looped over 'keys' and 'values' in two different
lists, merged them together, and down here
what we said was, if that key, if that key up here is 'age', skip it.
Otherwise, if the key is anything else, if the key equals
literally anything else that does not match 'age', then add
it to your dictionary.
And that is a dictionary comprehension.
So what I would like you to do is get a little bit of experience with this one.
Dictionary comprehensions are a little less commonplace than
a list comprehension, but it's still wildly valuable in Python,
and you're going to run into these anyway, so you might as
well get some practice with it now.
So go ahead, give this a shot.
Don't forget, you can always download this Jupyter Notebook as well, and
play around with my code.
And once you think you have the hang of it, let's go ahead
and start that next lesson.