1 00:00:00,230 --> 00:00:11,120 In this tutorial we under to discuss about dictionary methods the dictionary clear method is used to 2 00:00:11,660 --> 00:00:16,710 delete all of the items in the dictionary so it clears a dictionary next. 3 00:00:17,090 --> 00:00:28,550 The get method the get method returns the value for a key if it exists in the dictionary so let's check 4 00:00:28,550 --> 00:00:30,620 this in the Jupiter notebook. 5 00:00:30,650 --> 00:00:37,370 We have discussed before that if we want to retrieve or access any value in a dictionary we can parse 6 00:00:37,400 --> 00:00:43,130 the corresponding key in square brackets like this. 7 00:00:43,170 --> 00:00:51,630 So if we pass the key a one it retrieves the corresponding value that has been mapped to a 1. 8 00:00:51,840 --> 00:01:01,920 Apart from this we also had the get method using the get method we can access these values in the dictionary. 9 00:01:02,010 --> 00:01:09,250 So in order to apply the get method on the dictionary dict to 1. 10 00:01:09,720 --> 00:01:20,880 This is defined my dict one dot get and the argument is the key that you want to access in the dictionary. 11 00:01:20,970 --> 00:01:28,370 So I want to access the value corresponding to the key a 2. 12 00:01:28,410 --> 00:01:30,480 So let's execute this. 13 00:01:31,830 --> 00:01:39,260 So this is the value that has been mapped to the key A2 frozen foods. 14 00:01:39,270 --> 00:01:44,760 Say for example you pass a key that is not present. 15 00:01:44,820 --> 00:01:55,110 That is not existing in the dictionary so letters was b to as the key and execute it the get method 16 00:01:55,110 --> 00:02:02,770 does not find the key that has been passed as an argument in the dictionary dict 1. 17 00:02:02,960 --> 00:02:07,470 It does not raise an edit but it returns None. 18 00:02:07,500 --> 00:02:13,960 Dickey is not found we can pass an optional argument that is returned. 19 00:02:13,980 --> 00:02:18,980 If the key is not found in the dictionary in the get method. 20 00:02:19,080 --> 00:02:32,820 So let's see this dict one board get and let us specify the key is B to and the optional argument would 21 00:02:32,820 --> 00:02:34,940 be C minus one. 22 00:02:34,980 --> 00:02:41,180 If the value is not found they get method would at dawn this value. 23 00:02:41,220 --> 00:02:46,320 The default value select execute this line of code. 24 00:02:46,400 --> 00:02:53,930 So since Dicky B2 is not found do you get method returns the default value minus 1. 25 00:02:53,930 --> 00:03:04,220 So in this case Python does not raise an exception but if you try to access any of the values using 26 00:03:04,220 --> 00:03:12,660 the square brackets say for example let's pass the key B 2 which is not present in dict 1. 27 00:03:12,890 --> 00:03:24,050 Here Python raises an exception which is the key error since B2 is not present in the dict 1 dictionary. 28 00:03:27,170 --> 00:03:38,660 The next method is the items matter the items method returns a list of key value pairs in our dictionary. 29 00:03:38,780 --> 00:03:45,900 So this is the format for the items method D door items. 30 00:03:45,920 --> 00:03:49,280 So once you execute this then this is the output. 31 00:03:49,280 --> 00:04:00,150 Here is a list of the items in the dictionary dict 1 and as you can see here the items method returns 32 00:04:00,150 --> 00:04:09,420 a list of tuples containing the key value PEDs in the dictionary dict 1 and the first item in each tuple 33 00:04:09,570 --> 00:04:22,550 is the key and the second item is the value and we have the method keys and use the keys method returns 34 00:04:22,550 --> 00:04:29,570 a list of keys in a dictionary whereas the right use method returns a list of values in a dictionary. 35 00:04:29,580 --> 00:04:33,920 This is how you apply keys method on the dictionary. 36 00:04:34,110 --> 00:04:39,880 It has written a list of keys that are present in this dictionary. 37 00:04:39,900 --> 00:04:49,000 Similarly we have the values method which returns a list of values that are present in the dictionary. 38 00:04:49,090 --> 00:04:52,370 The one next is the pop method. 39 00:04:53,580 --> 00:04:57,140 The part method removes a key from a dictionary. 40 00:04:57,150 --> 00:05:06,480 If it is present and returns it value so I am now calling the pop method on the dictionary dict 1 and 41 00:05:06,480 --> 00:05:17,310 in passing the key a 4 so pop method is going to remove this entry from the dictionary Dec. 1. 42 00:05:17,670 --> 00:05:26,740 Let us now display the dictionary dict one as you can see a Ford has been removed from the dictionary 43 00:05:27,770 --> 00:05:37,060 say for example we want to pop an item which is not present in the dictionary say very positive key 44 00:05:37,060 --> 00:05:40,770 B to which does not exist in the dictionary dict 1. 45 00:05:41,950 --> 00:05:52,900 So the Bob method returns the exception key error the can also pass a default value that would be returned 46 00:05:52,930 --> 00:06:02,530 if the key is not existing in the dictionary similar to what we have been in the get method so dict 47 00:06:02,550 --> 00:06:12,380 one DA Bob let pass be 2 which is not existing in the dictionary and let's see if it does not find this 48 00:06:12,380 --> 00:06:22,910 value though output should be minus 1 so instead of raising an exception or throwing an error the pop 49 00:06:22,910 --> 00:06:28,620 method returns the default value minus one that we have past as an argument. 50 00:06:28,850 --> 00:06:36,550 Next is the pop item method it removes a key value pair from a dictionary. 51 00:06:36,920 --> 00:06:46,970 So when you call depart by the method on a dictionary the method pop item removed as a random or arbitrary 52 00:06:46,970 --> 00:06:57,560 key value pair from the dictionary so if 5 has been removed the key a 5 it now only has the dictionary 53 00:06:57,560 --> 00:07:07,520 dict one only has three keys a one a two and a three next as the update method the update method is 54 00:07:07,520 --> 00:07:15,970 used to march or dictionary but another dictionary or with an eye dribble of key while you bet. 55 00:07:16,070 --> 00:07:23,480 Say for example I want to march the dictionary is dict 1 and 2 using the update method. 56 00:07:23,480 --> 00:07:32,420 So in this case you need to parse the dick to dictionary as an argument to the update method let's execute 57 00:07:32,420 --> 00:07:38,150 this line and then check if the dictionary dict 1 has been updated. 58 00:07:38,490 --> 00:07:49,660 OK as you can see the key value pairs in the dictionary d 2 has been added to dictionary dict 1. 59 00:07:49,730 --> 00:07:57,670 Say for example if a key is present in both the dictionary is dict 1 and Dec. 2. 60 00:07:57,680 --> 00:08:08,020 In that case the corresponding value indicate one for that key is updated to the value from dict 2. 61 00:08:08,120 --> 00:08:17,150 You can use the delete statement to delete any of your own key value pairs from a dictionary. 62 00:08:17,150 --> 00:08:25,890 So let's say you want to delete the key rate from the dictionary dict 1 so you can use the delete. 63 00:08:25,940 --> 00:08:38,300 Let's just play one so here you have the updated dictionary dict one bit with the key weight delete 64 00:08:38,340 --> 00:08:47,030 it we can use the Lent function to know the land of the dictionary so let's parse dict 1 to the length 65 00:08:47,030 --> 00:08:47,910 function. 66 00:08:48,690 --> 00:08:51,990 So there are seven keys in this dictionary. 67 00:08:52,290 --> 00:08:59,750 So the length function has returned seven and this brings us to the end of the topic. 68 00:08:59,790 --> 00:09:00,960 Dictionary methods.