1 00:00:00,270 --> 00:00:05,360 Hello and welcome to this tutorial on python dictionary. 2 00:00:05,370 --> 00:00:14,210 A dictionary consists of a collection of key value pets and each key value pair. 3 00:00:14,250 --> 00:00:19,610 Maps key toward associated revenue. 4 00:00:19,740 --> 00:00:29,400 So here I have defined keys such as name age class height and weight. 5 00:00:31,190 --> 00:00:39,760 And these are the values the keys and the values are separated by coolant. 6 00:00:39,800 --> 00:00:42,550 So age is 5. 7 00:00:42,620 --> 00:00:45,360 Class is Grade 1. 8 00:00:45,510 --> 00:00:50,650 Height is 3 feet and weight is 20 kilos. 9 00:00:50,840 --> 00:01:01,970 And a dictionary can be defined by enclosing comma separated key while you pairs in curly braces. 10 00:01:01,970 --> 00:01:15,400 So here we have a dictionary dict to 1 which maps the keys to the values you are given key can appear 11 00:01:15,520 --> 00:01:25,390 in a dictionary only once so duplicate keys are not allowed in a dictionary and an empty dictionary 12 00:01:25,750 --> 00:01:31,620 is represented with a pair of curly braces. 13 00:01:31,840 --> 00:01:33,800 Make this next. 14 00:01:34,030 --> 00:01:41,870 Dictionaries are mutable and are un ordered collections of data. 15 00:01:41,920 --> 00:01:50,990 You can add items or delete items from a dictionary without creating new copies so a dictionary can 16 00:01:51,110 --> 00:01:54,120 be changed in place. 17 00:01:54,150 --> 00:01:56,660 We have talked about lives before. 18 00:01:56,850 --> 00:02:01,020 Lists are an ordered collection of data. 19 00:02:01,020 --> 00:02:06,960 That means they retain the order of the items in which we insert it. 20 00:02:07,860 --> 00:02:19,750 If we want to access or retrieve the elements in the list they are accessed using index numbers coming 21 00:02:19,750 --> 00:02:21,020 to a dictionary. 22 00:02:21,010 --> 00:02:25,680 A dictionary is in an ordered collection of data. 23 00:02:25,720 --> 00:02:33,760 That means when you add or delete entries from a dictionary you won't be guaranteed that any sort of 24 00:02:33,880 --> 00:02:35,650 order will be maintained. 25 00:02:36,100 --> 00:02:44,300 And if you want to retrieve all value from a dictionary we can retrieve the value using Dickey. 26 00:02:44,350 --> 00:02:49,440 Say for example a one that is aisle one 2 and 3. 27 00:02:49,480 --> 00:02:58,960 I'll forward Dickies in the dictionary and if we want to retrieve any of these values we can retrieve 28 00:02:58,960 --> 00:03:00,680 using the keys. 29 00:03:00,680 --> 00:03:09,300 So the mapping of the values with Dickies remains fixed but not deep position. 30 00:03:09,310 --> 00:03:11,190 It might vary. 31 00:03:11,290 --> 00:03:19,870 So when we want to access elements in a dictionary we do not access elements based on the position we 32 00:03:19,870 --> 00:03:23,370 do it based on these guys. 33 00:03:23,410 --> 00:03:36,270 Next a dictionary key must be of the type immutable that means we can use an integer offload a string 34 00:03:36,390 --> 00:03:44,940 or a boolean as a dictionary key coming to the dictionary value it can be of any type. 35 00:03:45,060 --> 00:03:54,000 So it could include lists and other dictionaries as well and dictionaries like lists support nesting 36 00:03:54,300 --> 00:03:59,440 to any debt addiction any key in appear in a dictionary. 37 00:03:59,580 --> 00:04:08,180 Only once and a dictionary can be created with the burden dict function. 38 00:04:08,400 --> 00:04:19,480 So this is the python built in function and to this function we need to parse arguments which are a 39 00:04:19,480 --> 00:04:28,780 sequence of key value pairs say for example in these key value pairs have to be included in a list. 40 00:04:28,900 --> 00:04:34,450 Say for example I have the key value PID H. 41 00:04:34,710 --> 00:04:42,900 Tom next I have class as the key Grade 1 is devalued. 42 00:04:42,910 --> 00:04:45,790 This is another key value pair. 43 00:04:47,300 --> 00:04:52,610 And this is parsed in a list to the dict function. 44 00:04:52,760 --> 00:04:58,320 So let's check this in the Jupiter notebook. 45 00:04:58,320 --> 00:05:10,920 So here define a dictionary dict one with the key value pairs separated by comma and then finally enclosed 46 00:05:10,920 --> 00:05:12,150 in curly places. 47 00:05:12,690 --> 00:05:16,150 So this is how we can define a dictionary. 48 00:05:16,380 --> 00:05:19,180 Select let's display this dictionary dict. 49 00:05:19,190 --> 00:05:20,130 1. 50 00:05:20,160 --> 00:05:25,920 Let us also see how we can define a dictionary using the dict function. 51 00:05:25,920 --> 00:05:33,200 So we will define and not the dictionary dict to using this dict. 52 00:05:33,210 --> 00:05:44,290 Function so right now since we have not included any key value pairs this is going to be an empty dictionary. 53 00:05:44,430 --> 00:05:51,120 Now we will add a few key value pairs to the dictionary to do so. 54 00:05:51,110 --> 00:06:02,040 Here I have created the dictionary dict to using the dict function and I have passed a list of key value 55 00:06:02,040 --> 00:06:12,510 pairs as argument to the dict function so let's execute this and display the dictionary to do so. 56 00:06:12,510 --> 00:06:22,110 Here is the dictionary to do displayed a value is retrieved from a dictionary by specifying its corresponding 57 00:06:22,110 --> 00:06:25,140 key in square brackets. 58 00:06:25,410 --> 00:06:34,560 So we have a dictionary dict to one year and say for example we want to know the items that are stored 59 00:06:34,650 --> 00:06:37,600 in a one I won. 60 00:06:37,620 --> 00:06:48,900 So in order to retrieve the value in the key a one we need to parse the key in square brackets like 61 00:06:48,900 --> 00:06:51,640 this and then execute. 62 00:06:52,020 --> 00:07:02,100 So you have your value retrieve for the corresponding key so similarly you can retrieve other keys using 63 00:07:02,100 --> 00:07:04,420 the square brackets. 64 00:07:04,440 --> 00:07:11,690 Say for example you want to add a key value pair to an existing dictionary. 65 00:07:11,700 --> 00:07:21,950 Say for example this is the dictionary dict 1 and you want to add another aisle to the existing dictionary 66 00:07:21,990 --> 00:07:36,350 so you have a 5 and you want to assign the value data in a 5 so let's now display the dictionary dict 67 00:07:36,360 --> 00:07:37,260 1. 68 00:07:37,260 --> 00:07:40,940 So here we have the dictionary dict 1. 69 00:07:42,420 --> 00:07:47,480 The dictionary Key needs to be an immutable later type. 70 00:07:47,580 --> 00:07:51,420 Dictionary value can be of any type. 71 00:07:52,760 --> 00:08:04,020 So let us now create another dictionary which contained a list of elements as devalue so he could have 72 00:08:04,020 --> 00:08:15,320 defined another dictionary dict 3 which has only one key country and then the value is a list of elements. 73 00:08:15,360 --> 00:08:17,420 India China and Brazil. 74 00:08:17,490 --> 00:08:27,450 In order to access the value in this dictionary we need to give the dictionary dict 3 and in square 75 00:08:27,480 --> 00:08:31,850 brackets pass the key and the key is country. 76 00:08:31,980 --> 00:08:41,430 And for example if you want to retrieve any of these elements in the list we need to add an additional 77 00:08:41,430 --> 00:08:44,520 index to this or dict 3. 78 00:08:45,450 --> 00:08:55,080 The first index contains the key country and within this key we want to the dream. 79 00:08:55,100 --> 00:08:57,140 The second element that is China. 80 00:08:57,230 --> 00:09:05,500 So the index for this element in this particular list is 1. 81 00:09:05,520 --> 00:09:10,810 So this is how you retrieve the elements in a sublease. 82 00:09:10,830 --> 00:09:17,830 Another example that a dictionary has another dictionary defined in it. 83 00:09:18,270 --> 00:09:29,980 So here we have the dictionary dict Ford which has one key type and the value is another dictionary 84 00:09:30,280 --> 00:09:33,060 which is enclosed in curly braces here. 85 00:09:33,910 --> 00:09:46,440 So in order to access the value which is assigned to type which is key parse the key in these square 86 00:09:46,470 --> 00:09:51,950 brackets and here you have the dictionary. 87 00:09:52,260 --> 00:10:04,740 Now in order to access these sub dictionary add an additional index and here you parse parse the key 88 00:10:04,800 --> 00:10:11,290 immutable which is defined in the sub dictionary. 89 00:10:11,310 --> 00:10:19,470 Now when you execute this you have the value that is assigned to the key immutable.