1 00:00:01,040 --> 00:00:01,790 ‫Welcome back. 2 00:00:01,790 --> 00:00:08,180 ‫And the last video we looked at dictionaries and we learn how to create dictionaries or assign values 3 00:00:08,180 --> 00:00:10,880 ‫to dictionaries, how to iterate through dictionaries. 4 00:00:10,880 --> 00:00:19,370 ‫We saw how to use this element AI in order to get the ID, so to speak, of a dictionary key item or 5 00:00:19,520 --> 00:00:20,660 ‫of each item. 6 00:00:20,660 --> 00:00:26,390 ‫And now we're going to look at how we can update and remove data from a dictionary. 7 00:00:26,870 --> 00:00:29,030 ‫So it's actually quite trivial. 8 00:00:29,180 --> 00:00:32,540 ‫So let's say we have the following situation. 9 00:00:32,540 --> 00:00:35,900 ‫We want to update a specific key. 10 00:00:35,900 --> 00:00:37,580 ‫So we want to update. 11 00:00:38,460 --> 00:00:42,960 ‫A position at a specific key, and that will be a little further down here. 12 00:00:43,670 --> 00:00:46,400 ‫So here we have the H.R. key. 13 00:00:46,400 --> 00:00:56,060 ‫That is the one that we want to update so we can check if there is, in fact, an employee in the directory 14 00:00:56,090 --> 00:00:57,770 ‫that contains this key. 15 00:00:58,640 --> 00:01:06,680 ‫So we check if this key to update in this case HR exists and if it exists, then we want to execute 16 00:01:06,680 --> 00:01:07,490 ‫some code. 17 00:01:07,880 --> 00:01:14,060 ‫And what we want to do is we want to replace Laura with a different employee. 18 00:01:14,120 --> 00:01:20,460 ‫So now Laura will be replaced with a new employee called in order to update an array. 19 00:01:20,480 --> 00:01:30,680 ‫We can just say the dictionary name, then the key name where we want to update something and then assigning 20 00:01:30,680 --> 00:01:34,700 ‫the value that we want to put into its place. 21 00:01:34,700 --> 00:01:38,110 ‫So at this position that is going to be the key. 22 00:01:38,120 --> 00:01:39,080 ‫h.R. 23 00:01:39,170 --> 00:01:42,440 ‫We are going to put this new employee object. 24 00:01:42,500 --> 00:01:49,640 ‫So we're going to say now instead of having the old person in hr, which was laura, we're going to 25 00:01:49,640 --> 00:01:52,160 ‫replace her with the younger Erika. 26 00:01:53,510 --> 00:01:56,900 ‫So that's what this code will do for us. 27 00:01:57,830 --> 00:02:05,630 ‫So that's how you can update an entry, basically dictionary name, key name in square brackets, and 28 00:02:05,630 --> 00:02:07,420 ‫then whatever you want to assign to it. 29 00:02:07,430 --> 00:02:14,600 ‫Now, the value that you're assigning to it has to, of course, follow the same type that the dictionary 30 00:02:14,600 --> 00:02:15,550 ‫was defined with. 31 00:02:15,560 --> 00:02:21,530 ‫So in our case, it has to be of type employee and this is in fact of type employee. 32 00:02:23,040 --> 00:02:28,870 ‫So now we can, of course also print that into our console like so employee withdrawal. 33 00:02:28,890 --> 00:02:33,720 ‫Key zero was updated at the position key to update. 34 00:02:34,560 --> 00:02:38,880 ‫So in this case ad hr was updated and then. 35 00:02:39,720 --> 00:02:40,800 ‫If that didn't work. 36 00:02:40,800 --> 00:02:49,470 ‫We can, of course, also print an error message here saying sorry the employee not found with this 37 00:02:49,470 --> 00:02:49,980 ‫key. 38 00:02:51,840 --> 00:02:54,240 ‫And like so. 39 00:02:56,420 --> 00:02:58,940 ‫And I have one too many, I think. 40 00:02:58,940 --> 00:02:59,170 ‫Like. 41 00:02:59,180 --> 00:03:00,260 ‫So this will. 42 00:03:01,200 --> 00:03:02,070 ‫Fix the error. 43 00:03:02,790 --> 00:03:05,940 ‫So let's run this real quick to see how we can update it. 44 00:03:05,940 --> 00:03:11,790 ‫And you see here at the very top, employee with role key HR was updated. 45 00:03:11,820 --> 00:03:13,950 ‫Now let's look at the HR person. 46 00:03:13,950 --> 00:03:14,940 ‫Where is she? 47 00:03:15,210 --> 00:03:16,140 ‫She's here. 48 00:03:16,140 --> 00:03:21,300 ‫It's like age 26 with a salary of 35,000 roughly. 49 00:03:21,560 --> 00:03:22,110 ‫Okay. 50 00:03:22,110 --> 00:03:28,770 ‫So the update worked and we can see in our list of all updates or all employees because that's where 51 00:03:28,770 --> 00:03:30,630 ‫we got all the employees from. 52 00:03:33,050 --> 00:03:40,460 ‫So now let's say we want to remove an item from our dictionary because here this is the for each group 53 00:03:40,460 --> 00:03:47,750 ‫that we use to add all of the items, all of the employees to our dictionary, then we edit it one. 54 00:03:47,750 --> 00:03:54,410 ‫So this is where we update and now let's look at how we can remove. 55 00:03:54,650 --> 00:04:01,190 ‫And in order to remove, we also need to have a string that will be the key that we want to replace 56 00:04:01,190 --> 00:04:08,510 ‫because well, we have a key value pair in our case that the key is a string and that's what we use 57 00:04:08,510 --> 00:04:08,990 ‫here. 58 00:04:09,260 --> 00:04:14,900 ‫And then we can also check if that actually works. 59 00:04:14,900 --> 00:04:23,390 ‫So the thing is the remove method, if you look at it, it actually has or it returns a boolean. 60 00:04:23,480 --> 00:04:29,660 ‫So hovering over remove, you will see returns a boolean, which means that it either removed it or 61 00:04:29,660 --> 00:04:31,160 ‫it didn't remove anything. 62 00:04:31,580 --> 00:04:39,890 ‫So if it removed something, what we're going to do is we're just going to say that at the key in this 63 00:04:39,890 --> 00:04:46,040 ‫case, in turn, the person was removed, maybe their internship was over, let's say after six months 64 00:04:46,040 --> 00:04:46,820 ‫or whatever. 65 00:04:46,880 --> 00:04:52,340 ‫Now we can delete that intern from our database. 66 00:04:52,790 --> 00:04:54,170 ‫So let's run this again. 67 00:04:54,170 --> 00:04:56,480 ‫In our case, not database, but dictionary. 68 00:04:57,020 --> 00:04:57,140 ‫Okay. 69 00:04:57,260 --> 00:05:01,730 ‫So here at the top, you see interne was removed and now let's see if we can find an intern. 70 00:05:01,730 --> 00:05:06,230 ‫And I believe we don't have an intern in our database here. 71 00:05:06,290 --> 00:05:13,760 ‫Okay, so we have the CEO, the HR person, the secretary, as well as the lead developer. 72 00:05:16,220 --> 00:05:20,990 ‫And now, of course, if it didn't work, we can have this statement. 73 00:05:20,990 --> 00:05:26,420 ‫So if no, then print an error message saying no employees file with this key. 74 00:05:26,420 --> 00:05:31,910 ‫And the key that we are looking at is the key to remove, which will be our internal key, which is 75 00:05:31,910 --> 00:05:33,110 ‫basically this one here. 76 00:05:33,110 --> 00:05:37,130 ‫So I hope you recall that we used the. 77 00:05:38,010 --> 00:05:42,060 ‫Role of the person as their key in the company. 78 00:05:42,240 --> 00:05:48,180 ‫So once again, this example is really just to demonstrate the features of C-sharp that you have here. 79 00:05:48,270 --> 00:05:50,640 ‫This is not how you would write this. 80 00:05:50,640 --> 00:06:01,380 ‫So I would not recommend to use the role as the key for your data structure, because the role is usually 81 00:06:01,380 --> 00:06:02,910 ‫something that many people can have. 82 00:06:02,910 --> 00:06:07,140 ‫So you can have multiple people having the role of intern. 83 00:06:07,320 --> 00:06:12,710 ‫You can have multiple lead developers in a company or multiple junior developers and so forth. 84 00:06:12,720 --> 00:06:15,930 ‫So that's something you have to be super wary about. 85 00:06:16,080 --> 00:06:20,790 ‫Now, I would always recommend to use an ID as the. 86 00:06:21,520 --> 00:06:22,150 ‫Key. 87 00:06:22,600 --> 00:06:27,670 ‫So something like an integer where you assign an ID to every single employee. 88 00:06:27,670 --> 00:06:30,820 ‫But this was just to show you that you have a lot of flexibility. 89 00:06:30,820 --> 00:06:37,030 ‫You can use strings as your keys, you can use integers as your keys and so forth. 90 00:06:37,850 --> 00:06:38,180 ‫All right. 91 00:06:38,180 --> 00:06:40,160 ‫So that's it for dictionaries for now. 92 00:06:40,190 --> 00:06:41,480 ‫See you in the next video.