1
1

00:00:00,810  -->  00:00:02,330
<v ->Hi guys, and welcome back.</v>
2

2

00:00:02,330  -->  00:00:03,640
In this video, we're going to learn
3

3

00:00:03,640  -->  00:00:05,980
about dictionary comprehensions.
4

4

00:00:05,980  -->  00:00:07,830
A dictionary comprehension is very much
5

5

00:00:07,830  -->  00:00:09,570
like a list comprehension,
6

6

00:00:09,570  -->  00:00:11,980
but we get a dictionary at the end of it,
7

7

00:00:11,980  -->  00:00:14,510
so we need to be assigning key value pairs
8

8

00:00:14,510  -->  00:00:16,240
instead of only values.
9

9

00:00:16,240  -->  00:00:18,500
Let's say we've got a list of users,
10

10

00:00:18,500  -->  00:00:21,940
where each user information is stored in a tuple.
11

11

00:00:21,940  -->  00:00:25,130
So we have a list with four user tuples.
12

12

00:00:25,130  -->  00:00:29,010
Inside it, they have an ID, a unique identifying number
13

13

00:00:29,010  -->  00:00:30,250
for each user,
14

14

00:00:30,250  -->  00:00:33,810
a username, and a password.
15

15

00:00:33,810  -->  00:00:36,520
So, we want to create a mapping of usernames
16

16

00:00:36,520  -->  00:00:38,130
to user information.
17

17

00:00:38,130  -->  00:00:39,920
This is something that you'll be doing very often,
18

18

00:00:39,920  -->  00:00:42,930
especially if you're doing something like web applications
19

19

00:00:42,930  -->  00:00:44,060
and things like that.
20

20

00:00:44,060  -->  00:00:46,810
So we can do a username_mapping variable.
21

21

00:00:46,810  -->  00:00:48,790
Remember, this name here doesn't matter.
22

22

00:00:48,790  -->  00:00:49,803
You can pick whatever you want.
23

23

00:00:49,803  -->  00:00:53,370
And then, we're going to do a dictionary comprehension.
24

24

00:00:53,370  -->  00:00:57,480
So we'll do user[1]:user.
25

25

00:00:57,480  -->  00:01:02,430
This is our key and our value for user in users.
26

26

00:01:02,430  -->  00:01:04,140
And that is our for loop.
27

27

00:01:04,140  -->  00:01:06,980
So in a dictionary comprehension, the value that you're
28

28

00:01:06,980  -->  00:01:08,670
going to put into your new dictionary
29

29

00:01:08,670  -->  00:01:10,880
is actually a key value pair.
30

30

00:01:10,880  -->  00:01:13,030
And then you've got your for loop, just like you had
31

31

00:01:13,030  -->  00:01:15,030
in your list comprehensions.
32

32

00:01:15,030  -->  00:01:19,130
What this ends up doing is getting the username,
33

33

00:01:19,130  -->  00:01:22,820
and associating with it the entire user tuple
34

34

00:01:22,820  -->  00:01:24,560
for each of your users.
35

35

00:01:24,560  -->  00:01:27,087
So if we print(username_mapping) out,
36

36

00:01:27,087  -->  00:01:28,503
you'll see what I mean.
37

37

00:01:30,100  -->  00:01:33,868 line:15% 
You end up with a rather long dictionary, where the keys
38

38

00:01:33,868  -->  00:01:36,163 line:15% 
are the usernames.
39

39

00:01:37,360  -->  00:01:40,410 line:15% 
And the values are the entire tuple,
40

40

00:01:40,410  -->  00:01:44,493 line:15% 
the whole piece of information that we have about a user.
41

41

00:01:45,370  -->  00:01:47,300
Why can this be helpful?
42

42

00:01:47,300  -->  00:01:50,240
Well, imagine you know a user's username,
43

43

00:01:50,240  -->  00:01:51,950
and you want to get their information out.
44

44

00:01:51,950  -->  00:01:56,537
You just access, let's say, "Bob," in your username_mapping,
45

45

00:01:56,537  -->  00:01:59,040
and you've got the information out.
46

46

00:01:59,040  -->  00:02:03,430 line:15% 
So here, you would get back the entire of Bob's tuple.
47

47

00:02:03,430  -->  00:02:05,700
If you didn't have a mapping, you would
48

48

00:02:05,700  -->  00:02:07,770
have to do something like this.
49

49

00:02:07,770  -->  00:02:10,600
For user in users:
50

50

00:02:10,600  -->  00:02:15,190
Then you would have to check if user[1] = "Bob":
51

51

00:02:15,190  -->  00:02:17,580
and then you would print(user) out.
52

52

00:02:17,580  -->  00:02:20,140
So as you can see, this is rather complicated
53

53

00:02:20,140  -->  00:02:21,230
in comparison.
54

54

00:02:21,230  -->  00:02:23,850
Once you know what this means, of course.
55

55

00:02:23,850  -->  00:02:25,770
Let's say that you've got something like this
56

56

00:02:25,770  -->  00:02:29,900
in your application, and you want users to log in.
57

57

00:02:29,900  -->  00:02:32,030
What you would do is you would ask the user
58

58

00:02:32,030  -->  00:02:33,333
for their username,
59

59

00:02:34,630  -->  00:02:36,950
and then you would ask them for their password.
60

60

00:02:36,950  -->  00:02:39,193
So, you can do password_input.
61

61

00:02:41,340  -->  00:02:43,210
Now that you've got the username and password,
62

62

00:02:43,210  -->  00:02:47,130
all you have to do is go into the mapping and get the user
63

63

00:02:47,130  -->  00:02:48,920
for their username.
64

64

00:02:48,920  -->  00:02:50,409
So we can say something like,
65

65

00:02:50,409  -->  00:02:54,237
username, password = username_mapping[username_input],
66

66

00:02:55,829  -->  00:03:00,190
and this is going to get the user information tuple,
67

67

00:03:00,190  -->  00:03:03,960
such as this one, and it's going to unpack it,
68

68

00:03:03,960  -->  00:03:06,550
or de-structure it into these three variables.
69

69

00:03:06,550  -->  00:03:09,380
The underscore variable, because we don't care about the ID.
70

70

00:03:09,380  -->  00:03:12,050
The username variable, that will be "Bob."
71

71

00:03:12,050  -->  00:03:15,350
And the password variable, that will be "password."
72

72

00:03:15,350  -->  00:03:19,250
Then, if the password_input = password that we found
73

73

00:03:19,250  -->  00:03:22,990
in our sort of, "database," on our list,
74

74

00:03:22,990  -->  00:03:25,773
then we can say that your details are correct.
75

75

00:03:26,850  -->  00:03:29,163
And otherwise, we can say something else.
76

76

00:03:32,430  -->  00:03:34,250
So this is an example of performing
77

77

00:03:34,250  -->  00:03:38,030
some sort of log-in using this structure here,
78

78

00:03:38,030  -->  00:03:39,240
this dictionary comprehension.
79

79

00:03:39,240  -->  00:03:41,420
This is really helpful because it saves you
80

80

00:03:41,420  -->  00:03:44,180
from having to do another for loop here, to make sure
81

81

00:03:44,180  -->  00:03:48,013
that you are using the right username for their input.
82

82

00:03:48,850  -->  00:03:50,928
I just wanted to show you how dictionary comprehensions
83

83

00:03:50,928  -->  00:03:54,450
work in Python, and I thought that an example like this one
84

84

00:03:54,450  -->  00:03:55,740
could be quite nice as well.
85

85

00:03:55,740  -->  00:03:57,800
So now that we know about dictionary comprehensions,
86

86

00:03:57,800  -->  00:03:59,740
we're ready to move on to the next video.
87

87

00:03:59,740  -->  00:04:00,690
I'll see you there!
