1 00:00:00,900 --> 00:00:01,733 -: By the way 2 00:00:01,733 --> 00:00:04,800 there is actually one more way that we've actually seen 3 00:00:04,800 --> 00:00:09,453 in lists how to look for an item in a dictionary. 4 00:00:10,770 --> 00:00:14,367 Remember how in lists we use the keyword, "in." 5 00:00:15,240 --> 00:00:17,160 We said something along the lines of, 6 00:00:17,160 --> 00:00:20,490 hey, is i in a list 7 00:00:20,490 --> 00:00:21,873 that contains i? 8 00:00:23,130 --> 00:00:26,313 Or we even use a string like, hi. 9 00:00:27,150 --> 00:00:29,970 Well, you can do the same with dictionaries. 10 00:00:29,970 --> 00:00:34,970 I can say, does basket exist in user? 11 00:00:35,520 --> 00:00:37,323 And let's print this out, 12 00:00:40,770 --> 00:00:41,603 and run. 13 00:00:42,630 --> 00:00:46,470 And it's going to say, yep, basket does exist in user. 14 00:00:46,470 --> 00:00:47,303 What about, 15 00:00:48,420 --> 00:00:49,953 what about size? 16 00:00:50,790 --> 00:00:52,500 Does that exist? 17 00:00:52,500 --> 00:00:53,520 Nope, that's false. 18 00:00:53,520 --> 00:00:56,130 That does not exist in user. 19 00:00:56,130 --> 00:00:59,580 But here is where it gets really interesting. 20 00:00:59,580 --> 00:01:01,470 And this, again, 21 00:01:01,470 --> 00:01:05,250 is another dictionary method that we can use. 22 00:01:05,250 --> 00:01:07,920 And by the way, you can see that there's not 23 00:01:07,920 --> 00:01:12,920 that many dictionary methods that, well, dictionaries have. 24 00:01:13,530 --> 00:01:16,710 Luckily we're going to go over the main ones here. 25 00:01:16,710 --> 00:01:19,737 The one is called "keys", 26 00:01:21,000 --> 00:01:25,290 and keys simply checks the keys. 27 00:01:25,290 --> 00:01:27,120 What are the keys of user? 28 00:01:27,120 --> 00:01:30,453 So if I go, hello here, and I click run, 29 00:01:32,430 --> 00:01:36,690 well, no, keys are sorry on this side, not this. 30 00:01:36,690 --> 00:01:38,730 So this is a value. 31 00:01:38,730 --> 00:01:42,333 So if I want to check keys, I'd say age. 32 00:01:44,160 --> 00:01:45,390 Which is true. 33 00:01:45,390 --> 00:01:48,810 But if I want to check values, well, in that case 34 00:01:48,810 --> 00:01:52,710 if I do, hello, I get true. 35 00:01:52,710 --> 00:01:55,333 It's going to go over 36 00:01:55,333 --> 00:01:59,973 or what we call iterate over the values. 37 00:02:00,810 --> 00:02:05,220 Another one that's really useful is the items. 38 00:02:05,220 --> 00:02:09,120 And items is special because we've seen keys and values 39 00:02:09,120 --> 00:02:14,120 but items actually grabs the entire item. 40 00:02:14,310 --> 00:02:17,550 So the way we can actually see this is 41 00:02:17,550 --> 00:02:19,410 let's just remove 42 00:02:19,410 --> 00:02:20,253 and click run. 43 00:02:21,480 --> 00:02:24,150 Alright, this is interesting. 44 00:02:24,150 --> 00:02:29,150 I have dict items here and I grab, looks like a list. 45 00:02:31,440 --> 00:02:34,980 And in here we have a bracket basket 46 00:02:34,980 --> 00:02:38,430 1, 2, 3, greet, hello, and age 20. 47 00:02:38,430 --> 00:02:40,290 But there's some weird syntax here 48 00:02:40,290 --> 00:02:44,190 that we haven't seen before, and this is actually a couple. 49 00:02:44,190 --> 00:02:46,380 So we're going to hold off on this and talk 50 00:02:46,380 --> 00:02:48,210 about this a little bit more when we talk 51 00:02:48,210 --> 00:02:50,760 about our next data structure. 52 00:02:50,760 --> 00:02:54,240 But keys, values, and items, is an easy way 53 00:02:54,240 --> 00:02:58,355 for us to grab these things for us from our dictionary. 54 00:02:58,355 --> 00:02:59,610 Let's continue on 55 00:02:59,610 --> 00:03:02,700 with other dictionary methods that we might use. 56 00:03:02,700 --> 00:03:07,700 One is a very nice one, it's called, "clear." 57 00:03:07,950 --> 00:03:09,363 And if I run this, 58 00:03:10,830 --> 00:03:13,830 you see that I get an empty dictionary. 59 00:03:13,830 --> 00:03:18,830 What if I just run users dot clear here and run user? 60 00:03:22,230 --> 00:03:25,540 Well, it's still an empty object 61 00:03:26,430 --> 00:03:28,740 or an empty dictionary. 62 00:03:28,740 --> 00:03:31,320 So you can see here that clear doesn't actually 63 00:03:31,320 --> 00:03:32,580 return anything. 64 00:03:32,580 --> 00:03:35,850 It doesn't, well, it's just in place, 65 00:03:35,850 --> 00:03:38,040 removes whatever the dictionary has. 66 00:03:38,040 --> 00:03:40,503 So it just creates an empty dictionary. 67 00:03:42,090 --> 00:03:44,313 What about copy? 68 00:03:45,420 --> 00:03:50,280 Well, a copy allows us to copy a user 69 00:03:50,280 --> 00:03:53,130 so that if I print user 70 00:03:53,130 --> 00:03:55,680 and user two 71 00:03:55,680 --> 00:03:57,153 and I click run. 72 00:03:58,500 --> 00:04:03,390 I get two users each keys and values copied. 73 00:04:03,390 --> 00:04:04,890 But if I now, 74 00:04:04,890 --> 00:04:09,390 let's say clear the first user 75 00:04:09,390 --> 00:04:10,803 and I click run, 76 00:04:12,090 --> 00:04:13,560 the first user is empty 77 00:04:13,560 --> 00:04:15,990 but because I've copied the second user, 78 00:04:15,990 --> 00:04:18,813 the second user has the old information. 79 00:04:20,310 --> 00:04:22,960 And this is a concept that we've talked about before. 80 00:04:24,540 --> 00:04:26,370 Another useful command 81 00:04:26,370 --> 00:04:28,533 and let's just go back to having user, 82 00:04:29,550 --> 00:04:32,010 is user dot pop 83 00:04:32,010 --> 00:04:36,120 which removes a key and a value from the dictionary. 84 00:04:36,120 --> 00:04:37,170 So in our case, 85 00:04:37,170 --> 00:04:42,170 let's just say we want to remove the key that is age. 86 00:04:42,510 --> 00:04:45,843 So I'm going to say age, and if I run this, 87 00:04:46,830 --> 00:04:48,000 I get 20 88 00:04:48,000 --> 00:04:52,380 because pop returns the value of whatever got removed. 89 00:04:52,380 --> 00:04:57,380 But if I do print user now, you'll see that age does not 90 00:04:57,390 --> 00:05:00,360 exist anymore because we've popped it off. 91 00:05:00,360 --> 00:05:04,953 But pop, as you can see, removes the actual value, 92 00:05:05,940 --> 00:05:08,730 or returns the actual value, 20. 93 00:05:08,730 --> 00:05:12,270 Here's another really fun one, pop item. 94 00:05:12,270 --> 00:05:15,660 And honestly, this I haven't used much, 95 00:05:15,660 --> 00:05:17,370 but I just think it's a fun one. 96 00:05:17,370 --> 00:05:20,763 Pop item, if you guessed, well, you're not going to guess. 97 00:05:21,690 --> 00:05:23,350 Oh, and that's because 98 00:05:24,540 --> 00:05:25,710 I have to make sure I write it 99 00:05:25,710 --> 00:05:27,480 properly with lowercase. 100 00:05:27,480 --> 00:05:28,473 If I run this, 101 00:05:30,480 --> 00:05:31,440 which just happened. 102 00:05:31,440 --> 00:05:34,143 It randomly pops off something. 103 00:05:35,100 --> 00:05:36,690 One of the keys and the values. 104 00:05:36,690 --> 00:05:39,450 In this case, age 20 is removed. 105 00:05:39,450 --> 00:05:42,480 So the last item on the dictionary gets removed. 106 00:05:42,480 --> 00:05:45,810 But remember, a dictionary is an order. 107 00:05:45,810 --> 00:05:49,530 So if this was a massive, massive dictionary 108 00:05:49,530 --> 00:05:53,190 that, well, doesn't have any order to it, 109 00:05:53,190 --> 00:05:56,550 sometimes it removes some pair. 110 00:05:56,550 --> 00:05:58,470 As you can see, not the last one, 111 00:05:58,470 --> 00:06:02,040 but some pair of key value. 112 00:06:02,040 --> 00:06:04,080 So you have to be careful with this one. 113 00:06:04,080 --> 00:06:06,330 It doesn't necessarily just remove the last thing 114 00:06:06,330 --> 00:06:10,650 that you entered, but it might be useful on some occasions. 115 00:06:10,650 --> 00:06:13,560 Finally, we have something called, "update." 116 00:06:13,560 --> 00:06:16,290 And update simply updates, 117 00:06:16,290 --> 00:06:18,363 as the name suggests, a key value. 118 00:06:19,200 --> 00:06:21,210 So let's have a look here. 119 00:06:21,210 --> 00:06:24,900 All we have to do is give it a new dictionary. 120 00:06:24,900 --> 00:06:28,020 So curly brackets, and just simply say, 121 00:06:28,020 --> 00:06:32,883 hey I want to update age to 55. 122 00:06:34,140 --> 00:06:35,703 If I click run, 123 00:06:37,762 --> 00:06:39,360 oh, and I forgot a bracket here. 124 00:06:39,360 --> 00:06:40,193 Let's try that again. 125 00:06:40,193 --> 00:06:41,103 If I click run, 126 00:06:42,990 --> 00:06:43,823 there you go. 127 00:06:43,823 --> 00:06:45,690 Age got updated. 128 00:06:45,690 --> 00:06:50,690 But if this was a key that doesn't exist like ages 129 00:06:51,060 --> 00:06:56,060 and I click run, it will still update with a new key item. 130 00:06:57,240 --> 00:07:00,420 So this is another really useful method. 131 00:07:00,420 --> 00:07:02,760 Alright, that's it for dictionaries. 132 00:07:02,760 --> 00:07:04,310 I'll see you in the next video.