1 00:00:00,420 --> 00:00:03,270 Now that we've seen lists as well as dictionaries 2 00:00:03,630 --> 00:00:08,630 I want to talk about a concept that you often see in both of these collection 3 00:00:09,300 --> 00:00:12,660 types and that's something called nesting. Now, 4 00:00:12,690 --> 00:00:15,930 if we imagine a list or a dictionary 5 00:00:15,960 --> 00:00:20,430 being something like a folder where lots of things can be stored inside it, 6 00:00:20,850 --> 00:00:25,850 then nesting lists and dictionaries is just a matter of putting one inside the 7 00:00:26,040 --> 00:00:28,200 other. For example, 8 00:00:28,380 --> 00:00:31,320 here;s a dictionary that is very simple, 9 00:00:31,320 --> 00:00:33,480 it's only got one key-value pair. 10 00:00:34,170 --> 00:00:39,170 Now we know that we can add multiple key-value pairs into the same dictionary by 11 00:00:40,110 --> 00:00:45,030 just adding a bunch of commas to separate them. Now, what if 12 00:00:45,180 --> 00:00:50,100 instead of having a simple value like a string or a number, 13 00:00:50,790 --> 00:00:54,270 I could also put a list as a value as well. 14 00:00:55,200 --> 00:00:56,033 Similarly, 15 00:00:56,040 --> 00:01:01,040 I could also use a dictionary as a value. In this case 16 00:01:01,170 --> 00:01:05,910 we've got a list as the value for key, this first one, 17 00:01:06,390 --> 00:01:11,390 and we've got a dictionary as the value for key2. Notice how this is a list and 18 00:01:13,410 --> 00:01:17,550 a dictionary nested inside another dictionary. 19 00:01:18,030 --> 00:01:20,490 The structure gets a little bit more complex, 20 00:01:21,090 --> 00:01:26,070 but it gives us more flexibility when we're trying to store more complex pieces 21 00:01:26,070 --> 00:01:29,580 of data. Coming back to our code, 22 00:01:30,000 --> 00:01:34,050 let's go ahead and create some sample dictionaries to see what this actually 23 00:01:34,050 --> 00:01:36,600 looks like in real life. For example, 24 00:01:36,600 --> 00:01:41,600 this could be a dictionary that contains the country as the key and the city, 25 00:01:43,350 --> 00:01:47,370 that is the capital, as the value. So for France, 26 00:01:47,400 --> 00:01:49,980 that's Paris, for Germany that's Berlin. 27 00:01:50,340 --> 00:01:54,360 This is a very simple dictionary that you've seen already. Now, 28 00:01:54,360 --> 00:01:59,360 if I wanted to nest a list in a dictionary, 29 00:02:00,180 --> 00:02:04,650 then I would be able to represent even more complex data. For example, 30 00:02:04,650 --> 00:02:09,650 if I had a travel log where I was going to collect a dictionary of all the 31 00:02:11,220 --> 00:02:15,990 cities I had been for each of the countries I've traveled to. So for example, 32 00:02:16,560 --> 00:02:21,560 if I had traveled to France and I wanted to say that I've been to multiple 33 00:02:21,600 --> 00:02:24,570 cities, I can't simply just say Paris, 34 00:02:24,630 --> 00:02:29,630 and then Lille, Dijon, that doesn't really work because each key can only have one 35 00:02:33,090 --> 00:02:37,530 value. The only way that we can make these three pieces of data 36 00:02:37,560 --> 00:02:41,430 one value is by turning it into a list, like so. 37 00:02:42,120 --> 00:02:45,300 So now in our travel log dictionary 38 00:02:45,750 --> 00:02:47,850 as represented by the curly braces, 39 00:02:48,180 --> 00:02:53,180 we have one key value pair and it just so happens that 40 00:02:53,250 --> 00:02:55,920 the value in this case is a list. 41 00:02:56,850 --> 00:03:01,570 And of course you can go on and add as many entries as you would like, 42 00:03:01,930 --> 00:03:06,930 and still preserving this kind of structure of key being a string and the value 43 00:03:07,630 --> 00:03:10,180 being a list. Now, 44 00:03:10,210 --> 00:03:14,050 this idea of nesting isn't limited dictionaries by the way. 45 00:03:14,050 --> 00:03:17,260 You could also just nest a list in a list. 46 00:03:17,800 --> 00:03:20,200 So you could have a list which is A 47 00:03:20,260 --> 00:03:25,260 and then B, and then the third item just happens to be another list which has C and D. 48 00:03:26,140 --> 00:03:28,450 This is perfectly valid Python code 49 00:03:28,990 --> 00:03:33,990 but it's not quite as useful as nesting a list in a dictionary or a dictionary 50 00:03:34,420 --> 00:03:39,310 in a dictionary, because the way that data is structured. Now, 51 00:03:39,310 --> 00:03:43,420 what if you wanted to nest a dictionary in a dictionary? 52 00:03:44,570 --> 00:03:48,130 Let's say that we wanted to expand our travel log. 53 00:03:48,790 --> 00:03:53,790 And instead of just storing the cities that I visited in each country, 54 00:03:54,280 --> 00:03:59,280 what if I wanted to also keep track of how many visits I've made to that 55 00:03:59,920 --> 00:04:00,753 country? 56 00:04:00,880 --> 00:04:05,710 Or what if I wanted to actually label what this piece of data is? 57 00:04:05,800 --> 00:04:07,660 Because at the moment it's kind of like here's 58 00:04:07,660 --> 00:04:10,540 a bunch of cities that's associated with the country, 59 00:04:10,840 --> 00:04:13,150 but it doesn't really describe this list. 60 00:04:13,900 --> 00:04:16,450 And I'd like to give this to you as a challenge, 61 00:04:16,630 --> 00:04:20,800 have a go at changing the France entry so that it contains another dictionary. 62 00:04:21,310 --> 00:04:25,330 For the nested dictionary use the key cities_visited, 63 00:04:25,600 --> 00:04:29,320 and for the value you can keep the list of Paris, Lille and Dijon. 64 00:04:29,830 --> 00:04:32,110 Pause the video now and see if you can get that to work. 65 00:04:32,130 --> 00:04:32,963 Yeah. 66 00:04:36,150 --> 00:04:39,270 All right, so here's the solution. By nesting a dictionary 67 00:04:39,300 --> 00:04:43,860 we can label the Paris, Lille and Dijon lists as cities visited. 68 00:04:44,400 --> 00:04:46,860 All I need to do is create the string, 69 00:04:46,890 --> 00:04:51,180 "cities_visited" and add a colon after it. But there's one more step. 70 00:04:51,390 --> 00:04:55,950 We have to add a pair of curly braces so that France is associated with a single 71 00:04:55,950 --> 00:04:58,560 value, namely our nested dictionary. 72 00:04:59,430 --> 00:05:03,480 Now that we have a dictionary of the travel log, 73 00:05:03,510 --> 00:05:08,510 all the countries I've been to, and each country has a value that is a dictionary 74 00:05:09,870 --> 00:05:14,100 in itself and it can store multiple pieces of data 75 00:05:14,400 --> 00:05:17,220 including the cities I've visited in that country 76 00:05:17,550 --> 00:05:21,060 as well as things such as the total number of visits, 77 00:05:22,470 --> 00:05:27,470 which in this case would have a string as the key and a 78 00:05:27,570 --> 00:05:31,110 number of as the value. Now 79 00:05:31,110 --> 00:05:35,970 have a go at creating your own travel log or modifying the second part of the 80 00:05:35,970 --> 00:05:40,970 travel log so that you also have a dictionary nested in a dictionary and you're 81 00:05:41,730 --> 00:05:44,730 able to represent something like this, 82 00:05:44,730 --> 00:05:49,730 the number of cities you visited in a list and another additional piece of data, 83 00:05:49,950 --> 00:05:50,783 if you wanted to. 84 00:05:51,330 --> 00:05:52,163 Right, 85 00:05:56,100 --> 00:05:59,630 And hopefully would have done something similar to this. 86 00:06:00,380 --> 00:06:05,380 And so now our data structure is such that we've got a list nested inside a 87 00:06:07,280 --> 00:06:12,110 dictionary, which is in itself nested in another dictionary. 88 00:06:13,190 --> 00:06:16,850 Now that we've seen nesting lists inside dictionaries, dictionaries inside 89 00:06:16,850 --> 00:06:17,683 dictionaries, 90 00:06:17,690 --> 00:06:22,520 the last thing I want to show you is nesting a dictionary inside a list. 91 00:06:22,970 --> 00:06:27,740 So we could basically have multiple dictionaries inside a single list. 92 00:06:28,430 --> 00:06:33,430 So remember that lists are ordered and they're accessed by the positions inside the 93 00:06:33,440 --> 00:06:38,060 list. So this dictionary would be the item at index zero, 94 00:06:38,450 --> 00:06:41,240 this one at index one and so on and so forth. 95 00:06:42,110 --> 00:06:43,580 Whereas inside a dictionary, 96 00:06:43,610 --> 00:06:48,380 the items are accessed by their keys like this France or Germany. 97 00:06:49,280 --> 00:06:54,140 Now I want to do the last type which is nesting a dictionary 98 00:06:56,030 --> 00:06:57,080 in a list. 99 00:06:57,620 --> 00:07:02,030 And I'm going to be working with the same travel log that we had previously, 100 00:07:02,600 --> 00:07:07,160 but I want to change it so that instead of having this being one giant 101 00:07:07,160 --> 00:07:10,160 dictionary with key-value pairs, 102 00:07:10,490 --> 00:07:15,200 I want to change it so that each of these entries are a dictionary in itself. 103 00:07:15,650 --> 00:07:19,610 Instead of having France be the key of this dictionary here, 104 00:07:20,060 --> 00:07:25,060 I want the turn it into its own key-value pair. Going inside this dictionary, 105 00:07:25,340 --> 00:07:27,440 I'm going to create a new key 106 00:07:27,440 --> 00:07:32,440 which is called country and then put France as the value for that key. 107 00:07:34,250 --> 00:07:39,110 So now I have an entire dictionary that has three pieces of data. 108 00:07:39,380 --> 00:07:41,810 The country visited, the cities visited, 109 00:07:42,110 --> 00:07:46,190 and the total number of visits. Let's do the same for Germany as well. 110 00:07:48,140 --> 00:07:53,140 So now you can see we've got two dictionaries and instead of holding these two 111 00:07:53,810 --> 00:07:55,760 dictionaries inside a dictionary, 112 00:07:56,150 --> 00:08:01,130 I'm going to change it so that it's now a list. I'm going to turn these curly 113 00:08:01,130 --> 00:08:03,380 braces into square brackets 114 00:08:03,860 --> 00:08:08,860 and this way we can add as many of these dictionaries inside the list and 115 00:08:09,260 --> 00:08:10,970 iterate through them if we need to. 116 00:08:11,690 --> 00:08:15,710 When we've been creating dictionaries that only have one or two key-value pairs, 117 00:08:15,950 --> 00:08:18,380 we've kind of just kept it all on one line. 118 00:08:18,830 --> 00:08:22,190 But once you've got more and more key-value pairs in the dictionary, 119 00:08:22,580 --> 00:08:27,580 it's usually a good idea to go ahead and separate out each of the entries, 120 00:08:28,900 --> 00:08:29,733 Right? 121 00:08:32,200 --> 00:08:33,700 Like what I'm doing here. 122 00:08:34,809 --> 00:08:39,809 Now it's a little bit easier to see that we have a list which contains two 123 00:08:40,510 --> 00:08:44,440 items, and each item is a dictionary. 124 00:08:45,010 --> 00:08:50,010 Each dictionary has three key-value pairs and they all contain different types 125 00:08:50,590 --> 00:08:54,640 of data. The first one has a value which is a string, 126 00:08:55,020 --> 00:08:58,380 the second one holds a list and the third one holds a number. 127 00:08:58,910 --> 00:09:03,890 The data types inside a dictionary can be completely mixed up if you want it to. 128 00:09:04,280 --> 00:09:09,280 But what can't change is you still need a key and a value separated by a colon. 129 00:09:11,990 --> 00:09:15,920 Now the only downside to this is you want to make sure that you're actually 130 00:09:15,920 --> 00:09:19,520 aware that later on when you try to pull out the number of visits, 131 00:09:19,790 --> 00:09:24,020 it's actually a number that you're working with rather than a string or when 132 00:09:24,020 --> 00:09:26,060 you're trying to get hold of the cities visited 133 00:09:26,330 --> 00:09:29,600 it's a list rather than a dictionary, for example. 134 00:09:30,920 --> 00:09:34,610 So now that we've the various different types of nesting, 135 00:09:35,000 --> 00:09:38,720 it's time for you to actually really understand this and get to grips with this 136 00:09:38,720 --> 00:09:43,720 concept by completing a challenge yourself. In the next lesson, 137 00:09:43,790 --> 00:09:48,710 I've got a coding challenge for you based on this travel log and you'll need to 138 00:09:48,710 --> 00:09:51,760 use what you've learned so far to be able to complete it. 139 00:09:52,130 --> 00:09:55,130 So for all of that and more, I'll see you on the next lesson.