-: Hi guys, and welcome back.
In this video, we're going to learn
about dictionary comprehensions.
A dictionary comprehension is very much
like a list comprehension,
but we get a dictionary at the end of it,
so we need to be assigning key value pairs
instead of only values.
Let's say we've got a list of users,
where each user information is stored in a tuple.
So we have a list with four user tuples.
Inside it, they have an ID, a unique identifying number
for each user,
a username, and a password.
So, we want to create a mapping of usernames
to user information.
This is something that you'll be doing very often,
especially if you're doing something like web applications
and things like that.
So we can do a username_mapping variable.
Remember, this name here doesn't matter.
You can pick whatever you want.
And then, we're going to do a dictionary comprehension.
So we'll do user[1]:user.
This is our key and our value for user in users.
And that is our for loop.
So in a dictionary comprehension, the value that you're
going to put into your new dictionary
is actually a key value pair.
And then you've got your for loop, just like you had
in your list comprehensions.
What this ends up doing is getting the username,
and associating with it the entire user tuple
for each of your users.
So if we print(username_mapping) out,
you'll see what I mean.
You end up with a rather long dictionary, where the keys
are the usernames.
And the values are the entire tuple,
the whole piece of information that we have about a user.
Why can this be helpful?
Well, imagine you know a user's username,
and you want to get their information out.
You just access, let's say, "Bob," in your username_mapping,
and you've got the information out.
So here, you would get back the entire of Bob's tuple.
If you didn't have a mapping, you would
have to do something like this.
For user in users:
Then you would have to check if user[1] = "Bob":
and then you would print(user) out.
So as you can see, this is rather complicated
in comparison.
Once you know what this means, of course.
Let's say that you've got something like this
in your application, and you want users to log in.
What you would do is you would ask the user
for their username,
and then you would ask them for their password.
So, you can do password_input.
Now that you've got the username and password,
all you have to do is go into the mapping and get the user
for their username.
So we can say something like,
username, password = username_mapping[username_input],
and this is going to get the user information tuple,
such as this one, and it's going to unpack it,
or de-structure it into these three variables.
The underscore variable, because we don't care about the ID.
The username variable, that will be "Bob."
And the password variable, that will be "password."
Then, if the password_input = password that we found
in our sort of, "database," on our list,
then we can say that your details are correct.
And otherwise, we can say something else.
So this is an example of performing
some sort of log-in using this structure here,
this dictionary comprehension.
This is really helpful because it saves you
from having to do another for loop here, to make sure
that you are using the right username for their input.
I just wanted to show you how dictionary comprehensions
work in Python, and I thought that an example like this one
could be quite nice as well.
So now that we know about dictionary comprehensions,
we're ready to move on to the next video.
I'll see you there!