1 00:00:00,009 --> 00:00:03,549 Let's talk about dictionaries and just like with lists, 2 00:00:04,000 --> 00:00:09,680 you can modify, you can add remove elements in your dictionaries. 3 00:00:09,829 --> 00:00:14,010 However, the core difference between your lists and dictionaries is that 4 00:00:14,359 --> 00:00:18,020 unlike in your list, where you have individual elements or 5 00:00:18,129 --> 00:00:20,389 values or items 6 00:00:20,739 --> 00:00:26,149 in your dictionary, we're going to have or refer to as a key value pair. So 7 00:00:26,489 --> 00:00:30,510 as an example, I can have a dictionary called user info 8 00:00:30,979 --> 00:00:32,470 and now equals 9 00:00:33,419 --> 00:00:36,360 and I'm going to now use the curly braces. OK? 10 00:00:36,369 --> 00:00:38,659 We typically use the curly braces for dictionaries. 11 00:00:38,849 --> 00:00:40,200 So in here, 12 00:00:40,610 --> 00:00:44,279 I can have information like the username, 13 00:00:44,770 --> 00:00:46,389 which would be admin. 14 00:00:47,240 --> 00:00:48,619 So right here, 15 00:00:48,750 --> 00:00:55,990 username is going to be the key. While admin is going to be the value of the key, 16 00:00:56,119 --> 00:00:58,700 we can also have a password. 17 00:00:58,900 --> 00:00:59,529 OK? 18 00:00:59,990 --> 00:01:04,138 And let's say the password here is uh let me in just as an example 19 00:01:04,589 --> 00:01:08,470 and we could even have another one like let's say last 20 00:01:08,940 --> 00:01:10,059 login. 21 00:01:10,739 --> 00:01:12,000 OK. Last login 22 00:01:12,680 --> 00:01:13,720 colon 23 00:01:14,190 --> 00:01:20,980 and I can have a date, let's say 20 24-10 dash 22 24 00:01:21,279 --> 00:01:23,019 just as an example. All right. 25 00:01:23,279 --> 00:01:28,260 So do note that whenever you're adding your key value pairs in your dictionary. 26 00:01:29,180 --> 00:01:32,980 At the end of each line, you will have the comma. 27 00:01:33,190 --> 00:01:37,660 But then for the very last key value pair, there is no need to add the comma at the end. 28 00:01:37,669 --> 00:01:40,110 All right. So just take note of that. So 29 00:01:40,309 --> 00:01:42,940 this is what we refer to as a dictionary. 30 00:01:43,129 --> 00:01:45,110 All right, we have our key value pairs, 31 00:01:45,269 --> 00:01:48,669 username. The value is admin we have the key password. 32 00:01:48,680 --> 00:01:50,250 The value is let me in and of course, 33 00:01:50,260 --> 00:01:55,010 we have the key last login and then we have the date there as the value. 34 00:01:55,110 --> 00:01:56,739 So from here, 35 00:01:57,190 --> 00:02:01,010 I could decide to print out the information I could come down here 36 00:02:01,370 --> 00:02:02,610 and then say print. 37 00:02:03,379 --> 00:02:06,120 And now if I wanted to print, let's say the user name, 38 00:02:06,330 --> 00:02:09,399 I will first start by saying user underscore info. 39 00:02:09,889 --> 00:02:12,199 And now the regular brackets 40 00:02:12,500 --> 00:02:13,240 in here, 41 00:02:13,710 --> 00:02:14,800 I would simply 42 00:02:15,080 --> 00:02:16,679 say user name 43 00:02:17,460 --> 00:02:18,220 and that's it. 44 00:02:18,539 --> 00:02:21,639 So now if I was to run, you can see admin 45 00:02:21,770 --> 00:02:24,169 has been provided. So 46 00:02:24,369 --> 00:02:27,470 this is how you can print out the values 47 00:02:28,110 --> 00:02:31,660 all the keys in your dictionary, 48 00:02:31,740 --> 00:02:36,089 you simply state the key whose value you want to print and 49 00:02:36,449 --> 00:02:38,080 the program will do the rest. 50 00:02:38,929 --> 00:02:39,050 So 51 00:02:39,589 --> 00:02:44,279 now just like I said, with dictionaries, you can easily add or modify 52 00:02:44,690 --> 00:02:47,779 the elements in your dictionary. So for example, 53 00:02:47,929 --> 00:02:53,679 if I wanted to add some new information, like let's say the email of the user, 54 00:02:54,039 --> 00:02:56,679 all I would need to do right now is I can come down here 55 00:02:56,929 --> 00:02:59,800 and simply say user underscore info, 56 00:03:01,330 --> 00:03:04,300 ok? And now I will have the regular brackets 57 00:03:04,990 --> 00:03:08,699 and now I'll simply add the key here, which is going to be email 58 00:03:08,910 --> 00:03:10,250 and then very simply, 59 00:03:10,470 --> 00:03:12,429 I'm just gonna say equals 60 00:03:12,789 --> 00:03:18,259 and now I can add the email address right there. So let's just say admin at 61 00:03:18,580 --> 00:03:20,919 lab cyber.com. 62 00:03:22,190 --> 00:03:24,110 Alright? Just as an example, right? 63 00:03:24,309 --> 00:03:28,309 So if I was to come down in here right now and print 64 00:03:29,449 --> 00:03:31,229 the user info, 65 00:03:31,500 --> 00:03:32,869 let me just remove 66 00:03:33,929 --> 00:03:38,550 the key there. So we want to print out all the user information in here. 67 00:03:39,009 --> 00:03:40,389 I run the program 68 00:03:40,660 --> 00:03:43,889 and there it is, it's going to print out everything. So username is admin. 69 00:03:44,029 --> 00:03:48,380 The password is let me in the last login is this date and then the email ID is admin 70 00:03:48,529 --> 00:03:48,839 at 71 00:03:48,970 --> 00:03:52,910 lab cyber.com. We could also update, 72 00:03:53,350 --> 00:03:55,110 right. We could also update the 73 00:03:55,339 --> 00:03:57,149 information in our dictionary. 74 00:03:57,259 --> 00:03:59,139 So once again as an example, 75 00:03:59,550 --> 00:04:01,679 if I wanted to update, let's say the password, right? 76 00:04:01,690 --> 00:04:04,130 So I can say user underscore info. 77 00:04:04,529 --> 00:04:06,369 And now in brackets, 78 00:04:07,080 --> 00:04:10,500 of course, the key here is going to be password, right? So password 79 00:04:11,309 --> 00:04:14,470 and now I need to provide the new password. 80 00:04:14,690 --> 00:04:17,170 So let's call it sword fish, 81 00:04:18,329 --> 00:04:19,230 so fish. 82 00:04:20,088 --> 00:04:20,878 And 83 00:04:21,278 --> 00:04:23,259 there it is. If I run my program 84 00:04:23,468 --> 00:04:25,079 now, you can see that the password has 85 00:04:25,218 --> 00:04:27,428 changed to SWD fish. So 86 00:04:27,908 --> 00:04:29,789 just like with your list, you can 87 00:04:29,998 --> 00:04:34,299 add new key value pairs to your dictionary. 88 00:04:34,368 --> 00:04:37,549 You can also modify the values of existing keys. 89 00:04:37,558 --> 00:04:42,348 Also just like with lists, we do have a variety of methods 90 00:04:42,479 --> 00:04:45,748 that we can actually apply on to our 91 00:04:45,898 --> 00:04:46,748 dictionary. 92 00:04:47,019 --> 00:04:51,299 So the very first one here I want to talk about here is going to be the get method 93 00:04:51,410 --> 00:04:55,070 which will retrieve the value associated with a key. 94 00:04:55,369 --> 00:04:55,920 So 95 00:04:56,149 --> 00:04:57,160 for example, 96 00:04:57,890 --> 00:05:01,529 let me remove all this information in here. All right, 97 00:05:01,970 --> 00:05:05,670 if I wanted to print the user info 98 00:05:06,359 --> 00:05:07,940 or when I get the user name, 99 00:05:08,320 --> 00:05:10,339 the value of the key user name, 100 00:05:10,459 --> 00:05:14,420 all I need to do right here is I can say print user.info 101 00:05:14,730 --> 00:05:16,769 and then dot gets. 102 00:05:17,339 --> 00:05:19,570 All right. And now in brackets again, 103 00:05:20,019 --> 00:05:23,179 I will now add the key which is username. 104 00:05:23,549 --> 00:05:25,049 So this right here 105 00:05:25,299 --> 00:05:28,019 should print out the user name. If I run 106 00:05:28,220 --> 00:05:29,739 there, it is admin. 107 00:05:30,540 --> 00:05:34,279 So just like with your list that we, you know, we used a pen copy and so on, 108 00:05:34,290 --> 00:05:39,239 we have the exact same methods also for dictionary and get right here is one of them. 109 00:05:39,250 --> 00:05:43,119 Another method is going to be the keys where you can simply print out 110 00:05:43,359 --> 00:05:47,399 all the keys that we have in the dictionary. So let me remove 111 00:05:47,750 --> 00:05:50,679 all the information in here. So if I run 112 00:05:51,019 --> 00:05:56,799 there you go, we have all the keys which are username, password and last log in, 113 00:05:56,809 --> 00:05:59,720 we also have the opposite which would be values. 114 00:06:00,010 --> 00:06:03,829 So instead of keys, I'm going to change this one over here to values. And if I run. 115 00:06:03,920 --> 00:06:06,510 Now we're going to have all the values listed 116 00:06:06,660 --> 00:06:12,980 in the output. Now, we could also create a loop that will go through our dictionary. 117 00:06:12,989 --> 00:06:15,109 And how can we do this? Well, 118 00:06:15,239 --> 00:06:18,769 we can use the for loop because we have a dictionary. 119 00:06:18,779 --> 00:06:22,109 Remember that the four loops work well with lists, dictionaries and so on. 120 00:06:22,119 --> 00:06:24,119 So I'm gonna say four 121 00:06:24,459 --> 00:06:26,709 and now I'll have to say key. 122 00:06:26,940 --> 00:06:28,910 OK. So for key 123 00:06:29,529 --> 00:06:31,640 and then comma value 124 00:06:32,799 --> 00:06:33,570 in 125 00:06:34,299 --> 00:06:37,040 and now user underscore 126 00:06:37,179 --> 00:06:38,040 info 127 00:06:38,809 --> 00:06:40,510 and we have to add 128 00:06:40,859 --> 00:06:44,320 another method called items, 129 00:06:44,940 --> 00:06:47,750 items and now brackets colon. 130 00:06:48,320 --> 00:06:49,170 And there it is, 131 00:06:49,339 --> 00:06:53,709 this is how you would create your loop that will run through your dictionary. 132 00:06:53,720 --> 00:06:57,589 So it's gonna say four key comma value and then in 133 00:06:57,790 --> 00:06:59,429 the name of the dictionary and then you're gonna 134 00:06:59,720 --> 00:07:02,640 add the items method to it. 135 00:07:03,049 --> 00:07:05,489 And now all we have to do from here 136 00:07:05,670 --> 00:07:07,470 is to simply print out 137 00:07:07,709 --> 00:07:08,260 the 138 00:07:08,700 --> 00:07:09,600 information. 139 00:07:09,829 --> 00:07:11,920 So I'm going to say print 140 00:07:12,690 --> 00:07:14,829 and let's use the F string. So our 141 00:07:15,250 --> 00:07:17,869 regular brackets first 142 00:07:19,089 --> 00:07:21,829 and now the F string now 143 00:07:22,119 --> 00:07:23,040 coats 144 00:07:23,369 --> 00:07:24,459 curly 145 00:07:24,769 --> 00:07:27,359 braces to hold and display the key 146 00:07:28,209 --> 00:07:29,959 and I will have a colon. 147 00:07:30,149 --> 00:07:33,779 And now the other collibra is to hold what to hold the 148 00:07:33,890 --> 00:07:35,029 value. 149 00:07:35,350 --> 00:07:36,910 And now if I run 150 00:07:37,779 --> 00:07:38,489 there you go, 151 00:07:38,750 --> 00:07:43,529 username, admin password, let me in. And of course, the last login date right there. 152 00:07:43,790 --> 00:07:49,049 So you can also use loops to run through your dictionaries, your lists and so on. 153 00:07:49,260 --> 00:07:50,989 So that's it for dictionaries. 154 00:07:51,000 --> 00:07:55,440 Join me in the next video where we'll now take a look at what we refer to as nested 155 00:07:55,540 --> 00:07:57,130 dictionaries. I will see you then.