1 00:00:08,060 --> 00:00:11,965 Welcome back. So, we've seen ways to get 2 00:00:11,965 --> 00:00:15,055 a value associated with a particular key in a dictionary. 3 00:00:15,055 --> 00:00:18,610 So, for example, if we have this dictionary and we assign it to 4 00:00:18,610 --> 00:00:22,190 the variable inventory and it has four key value pairs. 5 00:00:22,190 --> 00:00:25,494 If we wanted the value associated with the key oranges, 6 00:00:25,494 --> 00:00:32,540 then we would say something like print, inventory sub oranges. 7 00:00:33,810 --> 00:00:38,830 But sometimes we don't want the value associated with just one key, 8 00:00:38,830 --> 00:00:42,820 we want the value associated with every key in that dictionary or 9 00:00:42,820 --> 00:00:47,410 we want to iterate over every key value pair in that dictionary. 10 00:00:47,410 --> 00:00:51,500 In order to do that, dictionaries have a set of methods that we'll find useful. 11 00:00:51,500 --> 00:00:55,715 So, the first method that we'll find useful is.keys. 12 00:00:55,715 --> 00:01:01,395 So, if we want to loop over every key in our dictionary by using a for loop. 13 00:01:01,395 --> 00:01:08,830 So, if I say, for, key in inventory.keys. 14 00:01:12,890 --> 00:01:17,480 Then, for now I'm just going to print out the key. 15 00:01:17,480 --> 00:01:20,340 So I'll say print key. 16 00:01:21,010 --> 00:01:23,300 When I run this code, 17 00:01:23,300 --> 00:01:27,425 then I'll see that I get all of the keys in my dictionary. 18 00:01:27,425 --> 00:01:30,230 Remember that dictionaries aren't ordered, 19 00:01:30,230 --> 00:01:34,130 so there are no guarantees what order I'll actually get these keys in. 20 00:01:34,130 --> 00:01:37,355 The only guarantee is that this for loop 21 00:01:37,355 --> 00:01:41,425 is going to run once for every key regardless of order. 22 00:01:41,425 --> 00:01:45,935 So, when I just print out the key then I can see that there's apples, bananas, 23 00:01:45,935 --> 00:01:50,270 oranges, and pears are all of the four keys in our dictionary. 24 00:01:50,270 --> 00:01:53,810 If I want to get the value associated with that key. 25 00:01:53,810 --> 00:02:00,400 I can say. "Key has the value". 26 00:02:00,400 --> 00:02:03,425 Then to get the value associated with the key, 27 00:02:03,425 --> 00:02:10,810 I use Inventory sub key. 28 00:02:10,810 --> 00:02:15,100 Now, notice here that I'm not putting key in quotation marks. 29 00:02:15,100 --> 00:02:19,360 In other words, what I want is I want the value of the variable key. 30 00:02:19,360 --> 00:02:25,245 I don't want to look for the value associated with the key, named key. 31 00:02:25,245 --> 00:02:26,820 So, like if this dictionary had 32 00:02:26,820 --> 00:02:30,230 a key named key 33 00:02:30,230 --> 00:02:36,165 associated with the value 50 and I put key in quotation marks then I would get 50. 34 00:02:36,165 --> 00:02:38,945 But here instead, I'm saying I want 35 00:02:38,945 --> 00:02:43,425 key the variable's value to be the key that I'm fetching. 36 00:02:43,425 --> 00:02:48,425 So, now we'll see apples has the value 430, 37 00:02:48,425 --> 00:02:51,790 bananas has the value 312, 38 00:02:51,790 --> 00:02:54,660 oranges has the value 525, 39 00:02:54,660 --> 00:02:58,350 and pears has the value 217. 40 00:02:58,350 --> 00:03:03,310 So, in order to actually get that list of keys into a list. 41 00:03:03,310 --> 00:03:06,005 One thing that I can do is I can just say, 42 00:03:06,005 --> 00:03:12,420 keys equals a list of inventory.keys. 43 00:03:14,440 --> 00:03:18,545 If I now print out the value of keys, 44 00:03:18,545 --> 00:03:23,435 then I should get a list of keys in our dictionary inventory. 45 00:03:23,435 --> 00:03:27,245 But remember again, that there's no guarantee about order in here. 46 00:03:27,245 --> 00:03:30,095 So, these keys could be in any order. 47 00:03:30,095 --> 00:03:32,390 The only guarantee is that I'm going to get 48 00:03:32,390 --> 00:03:36,095 a list that has all of the keys in some order. 49 00:03:36,095 --> 00:03:39,470 Now, notice here that when I got the list of 50 00:03:39,470 --> 00:03:43,775 keys I cast it to a list by calling the list function. 51 00:03:43,775 --> 00:03:47,180 The reason that we'll do that we're not going to get into too much 52 00:03:47,180 --> 00:03:50,630 yet but on a high level we always cast to 53 00:03:50,630 --> 00:03:53,900 a list because in Python Three 54 00:03:53,900 --> 00:03:58,640 Inventory.keys doesn't actually quite return a list directly. 55 00:03:58,640 --> 00:04:01,340 It returns something that we can actually iterate over, 56 00:04:01,340 --> 00:04:06,410 so we can put inventory.keys in our for loop but in order to actually get 57 00:04:06,410 --> 00:04:09,200 a list of keys we're always going to have to cast it 58 00:04:09,200 --> 00:04:13,050 to a list by calling the list function. 59 00:04:14,470 --> 00:04:19,639 If we wanted to iterate over inventory in a slightly less verbose way, 60 00:04:19,639 --> 00:04:23,115 we could just say for k in inventory. 61 00:04:23,115 --> 00:04:27,350 So, whereas here we're saying for key in inventory.keys 62 00:04:27,350 --> 00:04:33,260 here we're just saying for- I'll just rename this key to be consistent, 63 00:04:33,260 --> 00:04:36,305 for every key in inventory. 64 00:04:36,305 --> 00:04:41,800 When we say that then Python automatically assumes that we want to iterate over the keys. 65 00:04:41,800 --> 00:04:47,870 So, when I run this code then I say.key and then every key in 66 00:04:47,870 --> 00:04:50,915 our dictionary again no guarantees about 67 00:04:50,915 --> 00:04:55,080 order just that we'll loop through every single key that we have. 68 00:04:55,240 --> 00:05:00,215 Dictionaries have two other methods that are somewhat similar to.keys. 69 00:05:00,215 --> 00:05:05,725 So,.values rather than getting a list of keys gets a list of values, 70 00:05:05,725 --> 00:05:11,190 so 430, 312, 525, and 217. 71 00:05:11,190 --> 00:05:14,840 So,.values would give us a list that has all of 72 00:05:14,840 --> 00:05:18,670 these integers but again there are no guarantees about ordering. 73 00:05:18,670 --> 00:05:22,190 So, we know that it's going to be a list with these four items, 74 00:05:22,190 --> 00:05:26,810 we just don't know what order inventory.values is going to be in. 75 00:05:26,810 --> 00:05:33,025 Inventory.items instead gives us a list of key value pairs as tuples. 76 00:05:33,025 --> 00:05:37,170 So, inventory.items is going to give us a list. 77 00:05:37,170 --> 00:05:39,240 And the first item might 78 00:05:39,240 --> 00:05:47,820 be apples and 430. 79 00:05:47,820 --> 00:05:50,950 Again, I say might be because we don't know what the ordering is, 80 00:05:50,950 --> 00:06:00,950 but the second item might be oranges associated with the value 525, 81 00:06:00,990 --> 00:06:07,580 and then we might have bananas and so on. 82 00:06:07,800 --> 00:06:18,950 So,.item gives us a list of tuples where every tuple is a key value pair. 83 00:06:21,040 --> 00:06:25,055 So on line three we print out the value of 84 00:06:25,055 --> 00:06:29,630 inventory.values and on line four we print out the value of inventory. 85 00:06:29,630 --> 00:06:33,275 Items. So, let's see what these two lines output. 86 00:06:33,275 --> 00:06:37,800 I'm going to for now comment out lines six and seven. 87 00:06:38,260 --> 00:06:41,600 So, we can see that when we printed it out 88 00:06:41,600 --> 00:06:46,050 inventory.values then we got all of the values. 89 00:06:48,020 --> 00:06:52,430 They aren't necessarily going to be in the order that we actually created 90 00:06:52,430 --> 00:06:56,780 the dictionary in but in this case they just happened to be in that order. 91 00:06:56,780 --> 00:07:00,680 When recalled inventory.items and you can see that we have 92 00:07:00,680 --> 00:07:04,890 a list of tuples of key-value pairs. 93 00:07:04,890 --> 00:07:12,275 So here, this first tuple says that the value of the key apples is 430, 94 00:07:12,275 --> 00:07:18,165 the value of the key bananas is 312, and so on. 95 00:07:18,165 --> 00:07:23,420 We can also use the same inoperator that we saw on lists and strings on dictionaries. 96 00:07:23,420 --> 00:07:29,230 So, if we print out the value of this expression apples in inventory, 97 00:07:29,230 --> 00:07:33,900 then the value of this expression is going to be a Boolean. 98 00:07:35,350 --> 00:07:39,720 The value of that Boolean is going to be true, 99 00:07:39,820 --> 00:07:43,605 if this is a key in our dictionary. 100 00:07:43,605 --> 00:07:48,225 So, if apples is 101 00:07:48,225 --> 00:07:51,870 a key in 102 00:07:51,870 --> 00:07:59,590 inventory and false otherwise. 103 00:08:01,100 --> 00:08:05,300 Now, it's crucial here that I mention that it's true only 104 00:08:05,300 --> 00:08:08,795 if apples is a key in our dictionary. 105 00:08:08,795 --> 00:08:12,065 It can't be a value it has to be a key. 106 00:08:12,065 --> 00:08:16,660 So, when we print out the value of apples in inventory, 107 00:08:16,660 --> 00:08:20,855 then we can see here that we have a key-value pair where the key is apples. 108 00:08:20,855 --> 00:08:23,940 So, this should be true. 109 00:08:24,130 --> 00:08:29,425 If we print out the value of cherries in inventory, 110 00:08:29,425 --> 00:08:35,340 then if we look at our key-value pairs apples, bananas, oranges, 111 00:08:35,340 --> 00:08:39,035 pears, we don't see anything that has the key cherries, 112 00:08:39,035 --> 00:08:42,270 so this is going to be false. 113 00:08:43,450 --> 00:08:49,470 So, if we comment out this code and just run 114 00:08:49,470 --> 00:08:55,370 lines two and three then we should see the value of apples and inventory is true, 115 00:08:55,370 --> 00:08:58,980 the value of cherries and inventory is false. 116 00:09:00,470 --> 00:09:06,399 Now, we can also write code that depends on the values of these Boolean expressions. 117 00:09:06,399 --> 00:09:10,880 So, we can say, if our dictionary has the key bananas, by saying. 118 00:09:10,880 --> 00:09:13,310 If bananas in inventory. 119 00:09:13,310 --> 00:09:19,480 Again, this expression is a Boolean that's true if bananas is a key in our dictionary, 120 00:09:19,480 --> 00:09:21,970 in this case, bananas is a key. 121 00:09:21,970 --> 00:09:26,230 So, if that key is in our dictionary which in this case it is, 122 00:09:26,230 --> 00:09:30,055 then we print out the value of inventory sub bananas. 123 00:09:30,055 --> 00:09:32,560 Otherwise, if bananas is not a key, 124 00:09:32,560 --> 00:09:34,735 we say we have no bananas. 125 00:09:34,735 --> 00:09:41,595 So, here we're going to print out 312 which is the value associated with bananas. 126 00:09:41,595 --> 00:09:45,020 If I modified this key to be something else, 127 00:09:45,020 --> 00:09:48,395 I'll literally call it something else. 128 00:09:48,395 --> 00:09:51,920 Then, we would see we have no bananas, 129 00:09:51,920 --> 00:09:55,160 because bananas is no longer a key in our dictionary. 130 00:09:55,160 --> 00:09:59,390 Another method that we can use on dictionaries is.get. 131 00:09:59,390 --> 00:10:03,680 So,.get works almost just like indexing. 132 00:10:03,680 --> 00:10:11,450 So, we can say inventory.get apples in the value of this expression is going to be 133 00:10:11,450 --> 00:10:23,790 the same as the value of inventory sub apples, 134 00:10:26,990 --> 00:10:31,470 which in this case is going to be 430. 135 00:10:31,470 --> 00:10:37,490 So, if I just run line three and comment out lines four and six here, 136 00:10:38,340 --> 00:10:42,100 then I see that I get 430. 137 00:10:42,100 --> 00:10:48,170 Now, on line four we say print out inventory.get cherries. 138 00:10:48,170 --> 00:10:52,230 Now, notice here that cherries is not in our dictionary, 139 00:10:52,230 --> 00:10:56,895 so we don't have any key whose value is cherry. 140 00:10:56,895 --> 00:10:58,530 So, when we run this code, 141 00:10:58,530 --> 00:11:00,955 when we get the value none. 142 00:11:00,955 --> 00:11:05,100 This is the difference between.get and actually indexing. 143 00:11:05,100 --> 00:11:07,050 Because if we indexed here, 144 00:11:07,050 --> 00:11:13,120 so if we said print out inventory sub cherries, 145 00:11:13,340 --> 00:11:20,980 we would instead get a runtime error because cherries was not a key in our dictionary. 146 00:11:20,980 --> 00:11:25,750 If we instead called.get then we get kind of a softer error. 147 00:11:25,750 --> 00:11:31,195 So, instead of actually giving us a runtime error that stops our program.get 148 00:11:31,195 --> 00:11:38,230 says the value is none..get also takes an optional second argument, 149 00:11:38,230 --> 00:11:41,470 which is to say that if this key isn't 150 00:11:41,470 --> 00:11:45,250 there then this is what the value of that expression should be. 151 00:11:45,250 --> 00:11:48,280 So, here when we say inventory.get 152 00:11:48,280 --> 00:11:52,120 cherries and then we pass in a second argument of zero, 153 00:11:52,120 --> 00:11:53,645 then this is going to say, 154 00:11:53,645 --> 00:11:58,625 if cherries is in our dictionary as a key then get the value associated with it. 155 00:11:58,625 --> 00:12:01,325 If cherries is not a key in our dictionary, 156 00:12:01,325 --> 00:12:04,075 then just use this as the value. 157 00:12:04,075 --> 00:12:07,010 This isn't going to add a key value pair with cherries, 158 00:12:07,010 --> 00:12:11,270 it's instead just going to say the value of this overall expression should be zero. 159 00:12:11,270 --> 00:12:13,145 So, when we run our code, 160 00:12:13,145 --> 00:12:19,170 we see that now when we call inventory.get cherries with the optional argument zero here, 161 00:12:19,170 --> 00:12:23,375 we instead get zero because cherries isn't a key in our dictionary. 162 00:12:23,375 --> 00:12:26,225 If we had 999, 163 00:12:26,225 --> 00:12:29,180 then we would get 999. 164 00:12:29,180 --> 00:12:35,045 If we had a key cherries in our dictionary, 165 00:12:35,045 --> 00:12:37,035 but say it's five, 166 00:12:37,035 --> 00:12:39,720 then we would instead get five. 167 00:12:39,720 --> 00:12:42,940 That's all for now until next time.