1 00:00:07,910 --> 00:00:10,980 Welcome back. In this lesson, 2 00:00:10,980 --> 00:00:14,190 we're going to learn about a new Python type called, dictionaries. 3 00:00:14,190 --> 00:00:16,680 Dictionaries like strings, lists, 4 00:00:16,680 --> 00:00:19,050 and tuples are a collection of items. 5 00:00:19,050 --> 00:00:21,060 But unlike strings, lists or tuples, 6 00:00:21,060 --> 00:00:24,615 they're an unordered collection of items meaning that they don't have a first, 7 00:00:24,615 --> 00:00:25,950 second or third item, 8 00:00:25,950 --> 00:00:28,905 they're kind of a bag of key value pairs. 9 00:00:28,905 --> 00:00:31,020 In order to create a dictionary, 10 00:00:31,020 --> 00:00:32,610 we use curly braces. 11 00:00:32,610 --> 00:00:36,195 So, this expression creates an empty dictionary. 12 00:00:36,195 --> 00:00:41,810 We assign this empty dictionary to the variable English to Spanish or eng2sp. 13 00:00:41,810 --> 00:00:44,140 So, that's what line one does. 14 00:00:44,140 --> 00:00:49,550 In order to assign one key value pair to this dictionary, then we say, 15 00:00:49,550 --> 00:00:53,300 eng2sp, and then specify a key that we want, 16 00:00:53,300 --> 00:00:56,650 and then we set it equal to whatever value we want. 17 00:00:56,650 --> 00:00:59,735 So, if I go forward to line two, 18 00:00:59,735 --> 00:01:04,160 you can see that first eng2sp starts out as an empty dictionary, 19 00:01:04,160 --> 00:01:07,080 and then when we run line two, 20 00:01:09,260 --> 00:01:12,620 then we set one key value pair. 21 00:01:12,620 --> 00:01:14,150 So, the key here, 22 00:01:14,150 --> 00:01:18,415 is one, and the value is uno. 23 00:01:18,415 --> 00:01:21,825 Unlike lists, strings or tuples, 24 00:01:21,825 --> 00:01:25,435 dictionaries store key value pairs instead of just items. 25 00:01:25,435 --> 00:01:28,015 What that means, is that every dictionary, 26 00:01:28,015 --> 00:01:34,820 has items that contain one key and one value. 27 00:01:35,300 --> 00:01:40,805 You can think of the key as the thing that you use to actually access the value. 28 00:01:40,805 --> 00:01:43,724 So, for example, in a physical dictionary, 29 00:01:43,724 --> 00:01:45,210 the keys would be words, 30 00:01:45,210 --> 00:01:48,090 and values would be their definitions. 31 00:01:48,090 --> 00:01:50,744 In the context of Python dictionaries, 32 00:01:50,744 --> 00:01:53,100 values can be any Python object, 33 00:01:53,100 --> 00:01:55,870 and keys can be almost any Python object, 34 00:01:55,870 --> 00:01:58,210 but we'll get to that more in a bit. 35 00:01:58,210 --> 00:02:01,870 So here, we have the key one, 36 00:02:01,870 --> 00:02:04,940 associated with the value uno. 37 00:02:04,940 --> 00:02:08,265 That's because in this dictionary, eng2sp, 38 00:02:08,265 --> 00:02:10,560 we're going to associate English words, 39 00:02:10,560 --> 00:02:12,195 with their Spanish equivalent. 40 00:02:12,195 --> 00:02:13,990 We're going to use the English word says 41 00:02:13,990 --> 00:02:18,050 the keys and the Spanish equivalent as the value. 42 00:02:18,050 --> 00:02:19,980 So again, in our code, 43 00:02:19,980 --> 00:02:25,780 we first created an empty dictionary using curly braces and then we assign the value 44 00:02:25,780 --> 00:02:32,480 associated with the key one to be uno by saying eng2sp sub one, 45 00:02:32,480 --> 00:02:36,630 and notice that this is a string because we have quotation marks around it. 46 00:02:36,630 --> 00:02:39,210 So, the key string one, 47 00:02:39,210 --> 00:02:42,055 is the value of string uno. 48 00:02:42,055 --> 00:02:49,215 The next line associates the key two with the value dos, 49 00:02:49,215 --> 00:02:54,535 and you can see that our key value paired here it gets added to the dictionary. 50 00:02:54,535 --> 00:02:57,655 Again, one thing that I mentioned in the introduction 51 00:02:57,655 --> 00:03:00,660 was that dictionaries are unordered, 52 00:03:00,660 --> 00:03:02,580 and that's actually kind of important. 53 00:03:02,580 --> 00:03:05,440 So, there's no notion of what's the first item, 54 00:03:05,440 --> 00:03:07,420 the second item, the third item and so on. 55 00:03:07,420 --> 00:03:11,370 Instead, just think of dictionaries as kind of a bag of key value pairs. 56 00:03:11,370 --> 00:03:14,380 You don't know what order you're going to get them in 57 00:03:14,380 --> 00:03:18,095 but you know if you set two to dos and one to uno, 58 00:03:18,095 --> 00:03:19,780 that these key-value pairs will be 59 00:03:19,780 --> 00:03:23,630 associated with each other no matter what order they're in. 60 00:03:23,820 --> 00:03:25,980 On line four here, 61 00:03:25,980 --> 00:03:31,830 we set the value associated with the key three to be tres. 62 00:03:32,080 --> 00:03:34,645 So, you can see again, 63 00:03:34,645 --> 00:03:37,429 the order kind of changes around in our dictionary, 64 00:03:37,429 --> 00:03:41,135 but the important thing is that every key, 65 00:03:41,135 --> 00:03:43,685 so three is associated with tres, 66 00:03:43,685 --> 00:03:45,770 two is associated with dos, 67 00:03:45,770 --> 00:03:48,430 and one is associated with uno. 68 00:03:48,430 --> 00:03:50,740 Now, when we print out our dictionary, 69 00:03:50,740 --> 00:03:54,785 then we print out a list of key value pairs. 70 00:03:54,785 --> 00:03:59,750 So, here you can tell that this is a dictionary because we have curly braces, 71 00:03:59,750 --> 00:04:04,760 and then every key value pair is separated by a comma. 72 00:04:04,760 --> 00:04:07,775 So, we have two commas here, 73 00:04:07,775 --> 00:04:10,985 and our key value pairs are three, 74 00:04:10,985 --> 00:04:13,120 is associated with tres, 75 00:04:13,120 --> 00:04:17,915 and we can tell because here we have a colon between the key and the value. 76 00:04:17,915 --> 00:04:21,410 Two is associated with dos, 77 00:04:21,410 --> 00:04:23,135 and you can tell, again, 78 00:04:23,135 --> 00:04:25,535 because we have a colon between two and dos, 79 00:04:25,535 --> 00:04:30,075 and one is associated with uno. 80 00:04:30,075 --> 00:04:38,600 So, here we always have the key first and then we have colon and then the value. 81 00:04:38,600 --> 00:04:44,050 Every one of these key value pairs is separated out by commas, 82 00:04:47,540 --> 00:04:53,630 and all of this is wrapped in curly braces to specify that it's a dictionary. 83 00:04:53,630 --> 00:04:58,850 You can also set these key value pairs in line. 84 00:04:58,850 --> 00:05:00,800 So, whereas in this code, 85 00:05:00,800 --> 00:05:02,900 we set one to uno, 86 00:05:02,900 --> 00:05:06,960 on line two and two to dos on line three and so on, 87 00:05:06,960 --> 00:05:09,375 we can also set them all on line one. 88 00:05:09,375 --> 00:05:14,280 So, if we instead declared our dictionary like this, 89 00:05:14,330 --> 00:05:19,925 so here again, we can tell that this is a dictionary because we have curly braces, 90 00:05:19,925 --> 00:05:24,060 and then we have a list of key value pairs. 91 00:05:27,040 --> 00:05:32,060 Every one of these key value pairs is separated out by a comma. 92 00:05:32,060 --> 00:05:33,935 Then we have the key, 93 00:05:33,935 --> 00:05:40,160 so three associated with the value tres and we specify that using a colon. 94 00:05:40,160 --> 00:05:45,480 So, one colon uno, two colon dos. 95 00:05:45,480 --> 00:05:50,610 So, now when we run line one and we look at our frames and objects and we'll see 96 00:05:50,610 --> 00:05:56,700 that eng2sp is now associated with a dictionary that has three key value pairs. 97 00:05:56,700 --> 00:06:00,620 In order to look up the value associated with a particular key, 98 00:06:00,620 --> 00:06:01,970 we use square brackets. 99 00:06:01,970 --> 00:06:04,400 So, here on line one, 100 00:06:04,400 --> 00:06:07,370 we create a dictionary that has three key value pairs, 101 00:06:07,370 --> 00:06:10,720 just like before, and on line three, 102 00:06:10,720 --> 00:06:16,620 we assign value to be eng2sp sub two. 103 00:06:16,620 --> 00:06:19,715 So, in order to get a particular value, 104 00:06:19,715 --> 00:06:22,055 we first say the name of the dictionary, 105 00:06:22,055 --> 00:06:25,600 eng2sp, then we use square brackets. 106 00:06:25,600 --> 00:06:28,215 Then inside of the square brackets, 107 00:06:28,215 --> 00:06:32,190 we put the name of the key that we want to get the value for. 108 00:06:32,190 --> 00:06:35,115 So, here the key is two. 109 00:06:35,115 --> 00:06:36,855 If we look at our dictionary, 110 00:06:36,855 --> 00:06:41,960 we can see the key two is associated with the value dos. 111 00:06:41,960 --> 00:06:45,634 So, the value of this overall expression, 112 00:06:45,634 --> 00:06:52,250 eng2sp sub two is going to be the string dos. 113 00:06:52,250 --> 00:06:57,840 What that means is that when we print out value on line four, 114 00:06:57,840 --> 00:07:00,790 then we're going to print out dos. 115 00:07:03,620 --> 00:07:07,680 So, you can see that running line three and four, 116 00:07:07,680 --> 00:07:08,960 first, on line three, 117 00:07:08,960 --> 00:07:10,895 we assign value to be the string dos, 118 00:07:10,895 --> 00:07:13,655 which is the value of this expression, 119 00:07:13,655 --> 00:07:16,070 and then when we print out value, 120 00:07:16,070 --> 00:07:19,195 in our program output, we get dos. 121 00:07:19,195 --> 00:07:21,405 So, on line five, 122 00:07:21,405 --> 00:07:24,615 we print out eng2sp sub one, 123 00:07:24,615 --> 00:07:28,100 and the value of this expression is the value associated 124 00:07:28,100 --> 00:07:31,755 with the key one in our dictionary eng2sp, 125 00:07:31,755 --> 00:07:33,750 so if I look at the dictionary here, 126 00:07:33,750 --> 00:07:37,880 I can see that the value associated with the key one is uno. 127 00:07:37,880 --> 00:07:42,260 So, the value of this expression is the string uno, 128 00:07:42,260 --> 00:07:48,480 and that's what gets printed when we print out eng2sp, sub one. 129 00:07:48,700 --> 00:07:52,290 That's all for now until next time.