WEBVTT 1 00:00:00.290 --> 00:00:02.090 Hi guys and welcome back. 2 00:00:02.090 --> 00:00:05.071 In this video we're going to learn about dictionaries. 3 00:00:05.071 --> 00:00:08.510 When you look in a dictionary for a word definition, 4 00:00:08.510 --> 00:00:11.040 you're performing a key search. 5 00:00:11.040 --> 00:00:14.010 What that means is you know what you want to search for, 6 00:00:14.010 --> 00:00:17.090 such as the word algorithm say, 7 00:00:17.090 --> 00:00:19.410 and when you go into the dictionary and you find 8 00:00:19.410 --> 00:00:22.850 the word algorithm, you get back some values 9 00:00:22.850 --> 00:00:25.810 or the definitions of algorithm. 10 00:00:25.810 --> 00:00:29.010 That is more or less what a dictionary is in Python. 11 00:00:29.010 --> 00:00:33.310 A dictionary allows you to store keys and values 12 00:00:33.310 --> 00:00:35.440 and it's useful for when you know the key 13 00:00:35.440 --> 00:00:37.630 and you wanna get the value back. 14 00:00:37.630 --> 00:00:38.463 Let's have a look. 15 00:00:38.463 --> 00:00:41.500 Imagine you want a dictionary of friend ages, 16 00:00:41.500 --> 00:00:43.610 what you would do is you would use curly braces 17 00:00:43.610 --> 00:00:45.710 as you did with sets and then 18 00:00:45.710 --> 00:00:49.642 you would type Rolf, colon, 24. 19 00:00:49.642 --> 00:00:52.270 Here you've got your first dictionary. 20 00:00:52.270 --> 00:00:54.570 Of course a dictionary of a single element 21 00:00:54.570 --> 00:00:56.510 is hardly useful, you might as well just have it 22 00:00:56.510 --> 00:00:58.180 on its own piece of paper, 23 00:00:58.180 --> 00:01:00.910 so you can have multiple elements here 24 00:01:02.840 --> 00:01:04.120 like that. 25 00:01:04.120 --> 00:01:06.900 So here we have a dictionary of three elements 26 00:01:06.900 --> 00:01:08.803 Rolf, Adam and Anne. 27 00:01:09.700 --> 00:01:11.936 If you wanted to get Rolf's age, 28 00:01:11.936 --> 00:01:15.160 you need to know what you are looking for 29 00:01:15.160 --> 00:01:16.680 and that is the string Rolf, 30 00:01:16.680 --> 00:01:20.090 that's the key and then you would get back 24 31 00:01:20.090 --> 00:01:21.970 which is the value. 32 00:01:21.970 --> 00:01:24.330 Very similar to how a real dictionary works 33 00:01:24.330 --> 00:01:25.700 where you know what you're looking for, 34 00:01:25.700 --> 00:01:29.676 say algorithm and you get back the definitions of the word. 35 00:01:29.676 --> 00:01:33.000 If you wanted to find Rolf's age, 36 00:01:33.000 --> 00:01:35.590 you can do print and then friend ages 37 00:01:35.590 --> 00:01:39.170 which is the dictionary and then inside square brackets 38 00:01:39.170 --> 00:01:42.333 you put what you're looking for, so Rolf. 39 00:01:43.840 --> 00:01:47.330 Python will know that Rolf is a key in the dictionary 40 00:01:47.330 --> 00:01:49.773 and it will give you back 24, 41 00:01:51.044 --> 00:01:53.170 so this thing here will become the value 24. 42 00:01:53.170 --> 00:01:54.773 Let's run this and show you. 43 00:01:56.100 --> 00:01:58.470 You can see the 24 comes out because that is 44 00:01:58.470 --> 00:02:01.080 the value associated with the key Rolf 45 00:02:01.080 --> 00:02:02.283 in our dictionary. 46 00:02:04.890 --> 00:02:07.100 You can always add keys to a dictionary 47 00:02:07.100 --> 00:02:12.100 by doing friend ages and then something like Bob, equal 20. 48 00:02:13.520 --> 00:02:16.050 Now important here is that what you're doing 49 00:02:16.050 --> 00:02:18.740 is you are accessing a key of the dictionary 50 00:02:18.740 --> 00:02:21.349 and you are setting a value in it. 51 00:02:21.349 --> 00:02:23.770 I see some people trying to do something 52 00:02:23.770 --> 00:02:26.630 like this, this is invalid Python, 53 00:02:26.630 --> 00:02:29.973 you cannot do this because you are not accessing 54 00:02:29.973 --> 00:02:32.715 the key in the dictionary, you are trying 55 00:02:32.715 --> 00:02:36.120 to access the key and the value in one place 56 00:02:36.120 --> 00:02:37.173 which you can't do. 57 00:02:38.814 --> 00:02:40.670 Also forget about doing this, 58 00:02:40.670 --> 00:02:44.620 friend ages Bob colon 20, this won't work either 59 00:02:44.620 --> 00:02:46.550 because the colon is only used here 60 00:02:46.550 --> 00:02:49.530 inside a dictionary, it's not used for assignment 61 00:02:49.530 --> 00:02:50.540 of new values. 62 00:02:50.540 --> 00:02:52.340 So this is how you assign a value 63 00:02:52.340 --> 00:02:54.530 to a key in the dictionary, even if the key 64 00:02:54.530 --> 00:02:55.753 doesn't exist yet. 65 00:02:56.690 --> 00:02:59.000 Of course that means that you can change 66 00:02:59.000 --> 00:03:02.420 and existing key in the dictionary if you want. 67 00:03:02.420 --> 00:03:05.848 Say Rolf's birthday passes, you can set friend ages 68 00:03:05.848 --> 00:03:08.043 of Rolf to 25. 69 00:03:09.350 --> 00:03:11.770 Let's print friend ages and you'll see that 70 00:03:11.770 --> 00:03:13.653 Rolf is 25 and Bob is 20. 71 00:03:15.510 --> 00:03:18.793 There you have Bob at the end and Rolf as well. 72 00:03:19.830 --> 00:03:22.820 A difference between dictionaries and sets 73 00:03:22.820 --> 00:03:25.660 is that dictionaries do keep the order 74 00:03:25.660 --> 00:03:27.950 in which you added keys to them 75 00:03:27.950 --> 00:03:28.843 as of Python 3.7. 76 00:03:30.335 --> 00:03:33.480 But like in sets, you cannot have duplicate keys 77 00:03:33.480 --> 00:03:34.780 in a dictionary. 78 00:03:34.780 --> 00:03:37.162 Indeed, if you already have Rolf in a dictionary 79 00:03:37.162 --> 00:03:40.440 and you try to add Rolf again, 80 00:03:40.440 --> 00:03:42.430 all that's gonna happen is you're gonna change Rolf, 81 00:03:42.430 --> 00:03:43.580 you're not gonna add it again. 82 00:03:43.580 --> 00:03:45.760 So you cannot have duplicate keys 83 00:03:45.760 --> 00:03:48.203 but the order is kept, as of Python 3.7. 84 00:03:49.060 --> 00:03:51.163 Before that, the order was not guaranteed. 85 00:03:52.420 --> 00:03:55.200 Imagine you have a programme that stores information 86 00:03:55.200 --> 00:03:56.860 about your friends. 87 00:03:56.860 --> 00:03:58.680 If you have multiple friends and you wanna store 88 00:03:58.680 --> 00:04:00.892 multiple pieces of information about them, 89 00:04:00.892 --> 00:04:03.580 the best thing to do is to use a list 90 00:04:03.580 --> 00:04:06.220 or a tuple of dictionaries. 91 00:04:06.220 --> 00:04:08.280 So we will do friends equal 92 00:04:09.220 --> 00:04:12.104 and then a tuple and inside it we will put 93 00:04:12.104 --> 00:04:14.110 the name of our friend 94 00:04:16.040 --> 00:04:17.593 and the age of our friend. 95 00:04:20.100 --> 00:04:22.130 So what you've got here is a tuple 96 00:04:22.130 --> 00:04:24.010 with three elements inside it 97 00:04:24.010 --> 00:04:26.960 and it's very normal to define longer lists 98 00:04:26.960 --> 00:04:29.460 and tuples like this with the starting bracket 99 00:04:29.460 --> 00:04:31.070 and ending bracket in their own line 100 00:04:31.070 --> 00:04:35.990 and then indented with four spaces, the values. 101 00:04:35.990 --> 00:04:37.930 Putting four spaces, or indeed any number 102 00:04:37.930 --> 00:04:40.930 of spaces in front of a line is called indentation 103 00:04:40.930 --> 00:04:42.500 by the way. 104 00:04:42.500 --> 00:04:45.640 So what we've got here is a very typical use case 105 00:04:45.640 --> 00:04:48.611 of dictionaries which is to store information in a way 106 00:04:48.611 --> 00:04:51.324 that will be easily retrievable and in a way 107 00:04:51.324 --> 00:04:55.320 that is similar to other dictionaries that are related. 108 00:04:55.320 --> 00:04:56.940 Here's what I mean by that. 109 00:04:56.940 --> 00:04:59.700 If you wanted a friend's name, 110 00:04:59.700 --> 00:05:01.860 you know that you can access one of these elements 111 00:05:01.860 --> 00:05:05.050 and then the key name and that's gonna give you 112 00:05:05.050 --> 00:05:06.200 the name of the person. 113 00:05:07.290 --> 00:05:08.973 If you did this, 114 00:05:11.890 --> 00:05:13.960 which is what we had earlier, 115 00:05:13.960 --> 00:05:16.976 then you cannot access the name of each friend, 116 00:05:16.976 --> 00:05:19.640 you will need to know the name of each friend before 117 00:05:19.640 --> 00:05:21.640 you can access the value. 118 00:05:21.640 --> 00:05:23.453 So by doing something like this, 119 00:05:23.453 --> 00:05:27.410 what that allows us to do is say print friends zero 120 00:05:27.410 --> 00:05:29.873 name, for example. 121 00:05:30.740 --> 00:05:32.380 What we're doing here is we're accessing 122 00:05:32.380 --> 00:05:34.600 the first element of the tuple 123 00:05:34.600 --> 00:05:38.138 which is this dictionary and then the name property 124 00:05:38.138 --> 00:05:41.363 of the dictionary which is Rolf Smith. 125 00:05:42.740 --> 00:05:44.600 Notice that this is getting a little bit longer 126 00:05:44.600 --> 00:05:47.830 so something you can do is you can do something 127 00:05:47.830 --> 00:05:50.090 like friend is friends zero 128 00:05:50.090 --> 00:05:53.020 and then you can do print friend name. 129 00:05:53.020 --> 00:05:55.430 This is exactly the same thing but now 130 00:05:55.430 --> 00:05:57.630 you are splitting a variable out first 131 00:05:57.630 --> 00:05:59.610 so that it's a bit more readable. 132 00:05:59.610 --> 00:06:00.950 Because you can do this, 133 00:06:00.950 --> 00:06:02.930 and I'm actually going to revert back to it 134 00:06:02.930 --> 00:06:05.818 so it's a little bit easier to copy and paste, 135 00:06:05.818 --> 00:06:09.450 you can simply increase the number there 136 00:06:09.450 --> 00:06:12.140 zero one two, and you're always accessing 137 00:06:12.140 --> 00:06:15.310 the name property of the dictionary. 138 00:06:15.310 --> 00:06:18.374 And it's always gonna give you the friend's name. 139 00:06:18.374 --> 00:06:19.890 And this can be particularly useful when you have 140 00:06:19.890 --> 00:06:22.450 a lot of friends or a lot of data in your lists 141 00:06:22.450 --> 00:06:24.560 and your tuples and you want the data 142 00:06:24.560 --> 00:06:26.930 to be homogeneous so that you can always 143 00:06:26.930 --> 00:06:29.910 access the same key for each piece of data. 144 00:06:29.910 --> 00:06:31.871 You'll see more about why this is useful 145 00:06:31.871 --> 00:06:33.853 in the later sections of the course. 146 00:06:35.300 --> 00:06:37.450 I also wanted to tell you about another function 147 00:06:37.450 --> 00:06:40.060 which is called dict which is used to turn 148 00:06:40.060 --> 00:06:42.110 data into a dictionary. 149 00:06:42.110 --> 00:06:44.357 Here we've got a list of tuples 150 00:06:44.357 --> 00:06:47.460 where there are three elements in our list, 151 00:06:47.460 --> 00:06:49.543 these tuples, Rolf comma 24, 152 00:06:49.543 --> 00:06:53.450 Adam comma 30 and Anne comma 27. 153 00:06:53.450 --> 00:06:57.223 You can very easily turn this into a dictionary 154 00:06:57.223 --> 00:07:01.730 by passing it to the dict function here. 155 00:07:01.730 --> 00:07:04.470 So we're doing dict and then inside brackets 156 00:07:04.470 --> 00:07:07.190 put in friends, what you end up with 157 00:07:07.190 --> 00:07:09.280 is something like what we had at the start 158 00:07:09.280 --> 00:07:10.629 of the video. 159 00:07:10.629 --> 00:07:14.923 Rolf 24, Adam 30, Anne 27. 160 00:07:16.140 --> 00:07:18.470 So this can be useful because there are many 161 00:07:18.470 --> 00:07:20.410 different parts of Python that will give 162 00:07:20.410 --> 00:07:22.883 you data in this format instead of a dictionary 163 00:07:22.883 --> 00:07:26.110 and turning them into a dictionary like this 164 00:07:26.110 --> 00:07:27.390 can be really handy. 165 00:07:27.390 --> 00:07:29.730 Over the next few videos and the next few sections, 166 00:07:29.730 --> 00:07:31.774 we're going to be using this every now and then 167 00:07:31.774 --> 00:07:33.310 so I just wanted to introduce it to you now 168 00:07:33.310 --> 00:07:34.720 so you know how it works, 169 00:07:34.720 --> 00:07:36.250 but don't worry when we come to it, 170 00:07:36.250 --> 00:07:38.780 we will explain it further as well. 171 00:07:38.780 --> 00:07:40.040 Thanks for joining me in this video 172 00:07:40.040 --> 00:07:41.070 I hope you've learned something 173 00:07:41.070 --> 00:07:42.720 and I'll see you in the next one.