WEBVTT

0
00:00.200 --> 00:07.190
Now that we've seen lists as well as dictionaries, I want to talk about a concept that you often see

1
00:07.190 --> 00:11.690
in both of these collection types, and that's something called nesting.

2
00:12.080 --> 00:19.280
Now, if we imagine a list or a dictionary being something like a folder where lots of things can be

3
00:19.280 --> 00:26.600
stored inside it, then nesting lists and dictionaries is just a matter of putting one inside the other.

4
00:27.260 --> 00:31.300
For example, here's a dictionary that is very simple.

5
00:31.300 --> 00:33.640
It's only got one key / value pair.

6
00:33.850 --> 00:41.140
Now we know that we can add multiple key value pairs into the same dictionary by just adding a bunch

7
00:41.140 --> 00:43.000
of commas to separate them.

8
00:43.570 --> 00:52.840
Now what if instead of having a simple value like a string or a number, I could also put a list as

9
00:52.840 --> 00:54.520
a value as well.

10
00:54.910 --> 00:59.800
Similarly, I could also use a dictionary as a value.

11
01:00.040 --> 01:04.690
In this case, we've got a list as the value for key,

12
01:04.720 --> 01:10.330
this first one, and we've got a dictionary as the value for key2.

13
01:10.720 --> 01:17.770
Notice how this is a list and a dictionary nested inside another dictionary.

14
01:17.800 --> 01:24.120
The structure gets a little bit more complex, but it gives us more flexibility when we're trying to

15
01:24.120 --> 01:26.940
store more complex pieces of data.

16
01:27.840 --> 01:34.020
Coming back to our code, let's go ahead and create some sample dictionaries to see what this actually

17
01:34.020 --> 01:35.640
looks like in real life.

18
01:35.640 --> 01:43.820
For example, this could be a dictionary that contains the country as the key and the city that is the

19
01:43.820 --> 01:46.040
capital as the value.

20
01:46.220 --> 01:50.120
So for France, that's Paris, for Germany, that's Berlin.

21
01:50.150 --> 01:53.720
This is a very simple dictionary that you've seen already.

22
01:53.750 --> 02:02.900
Now, if I wanted to nest a list in a dictionary, then I would be able to represent even more complex

23
02:02.900 --> 02:03.620
data.

24
02:03.620 --> 02:12.020
For example, if I had a travel_log where I was going to collect a dictionary of all the cities I had

25
02:12.020 --> 02:14.510
been for each of the countries I've traveled to.

26
02:14.510 --> 02:22.250
So, for example, if I had traveled to France and I wanted to say that I've been to multiple cities,

27
02:22.250 --> 02:28.040
I can't simply just say, Paris, and then Lille, Dijon,

28
02:28.550 --> 02:34.000
that doesn't really work, because each key can only have one value.

29
02:34.030 --> 02:40.690
The only way that we can make these three pieces of data, one value, is by turning it into a list

30
02:40.690 --> 02:41.680
like so.

31
02:41.770 --> 02:51.100
So now in our travel_log dictionary, as represented by the curly braces, we have one key/value pair.

32
02:51.220 --> 02:56.040
And it just so happens that the value in this case is a list.

33
02:56.520 --> 03:01.740
And of course, you can go on and add as many entries as you would like,

34
03:01.740 --> 03:08.670
and still preserving this kind of structure of key being a string and the value being a list.

35
03:09.360 --> 03:16.770
Now I've got a challenge for you to test your understanding of how lists nested inside dictionaries

36
03:16.770 --> 03:17.760
might work.

37
03:17.790 --> 03:27.990
I want you to guess and figure out how can you print out the Lille string from this travel_log.

38
03:28.020 --> 03:36.510
So think about how you might access items in a dictionary, and then think about how you access items

39
03:36.510 --> 03:38.010
in a list.

40
03:38.010 --> 03:41.880
And the goal is just to print out this Lille.

41
03:41.910 --> 03:43.020
Pause the video.

42
03:43.020 --> 03:43.980
Give this a go.

43
03:45.770 --> 03:50.870
Okay, so let's first start with accessing this list.

44
03:50.870 --> 03:58.910
So if we go ahead and use our print statement to tap into the travel_log, we know that we can access

45
03:58.910 --> 04:04.040
the value of a dictionary by using the key.

46
04:04.040 --> 04:06.380
So we use the square brackets.

47
04:06.380 --> 04:10.930
And then we have to type the key which in this case is France.

48
04:10.930 --> 04:15.400
So if we print this right now we're getting hold of the list.

49
04:15.400 --> 04:17.740
And we know how to work with lists already.

50
04:17.740 --> 04:26.800
So if we want the item at index zero one then we just need another set of square brackets and add one

51
04:26.800 --> 04:27.670
in there.

52
04:27.670 --> 04:33.640
So now if we run this you can see we're able to get hold of this item.

53
04:33.940 --> 04:42.520
Now, we've previously seen that you can nest lists inside other lists, and you can end up with a many

54
04:42.520 --> 04:44.140
layered nested list.

55
04:44.140 --> 04:52.810
So for example, if I had a list that had the letters A and B, I can nest a list inside there at the

56
04:52.810 --> 04:54.160
index[2],

57
04:54.190 --> 04:56.080
So, 0 1  2.

58
04:56.110 --> 04:59.880
The third item in this list is another list.

59
04:59.880 --> 05:03.180
And I'm going to add C and D.

60
05:03.360 --> 05:06.540
So now we have what's called a 2D list,

61
05:06.540 --> 05:07.800
we have a list,

62
05:07.800 --> 05:10.620
and then we have a list inside that list.

63
05:10.620 --> 05:13.230
So here's another challenge for you.

64
05:13.260 --> 05:18.840
See if you can figure out how to print out this letter D from the nested list.

65
05:18.840 --> 05:24.420
Think about what we did before and think about what you've learned previously about nested lists,

66
05:24.450 --> 05:27.450
to complete this challenge. Pause the video now.

67
05:30.210 --> 05:34.530
Okay, so let's first access this list.

68
05:34.530 --> 05:41.790
So if we go ahead and print our nested list and then we put in the index of this list, it would be

69
05:41.790 --> 05:44.610
at 0, 1, 2.

70
05:44.640 --> 05:48.320
So let's put two inside the set of square brackets.

71
05:48.320 --> 05:50.960
I'm just going to comment this other print statement out.

72
05:50.960 --> 05:53.960
And now you can see we've got hold of C and D.

73
05:54.110 --> 06:00.650
So now that we've got that list how would we access D if we were working with this list.

74
06:00.650 --> 06:05.540
Well we would tap into the item at index 1.

75
06:05.540 --> 06:11.120
So what we know is this part of the code results in this list.

76
06:11.120 --> 06:17.770
Then to get hold of D, all we need to do is add another set of square brackets and then add 1 as

77
06:17.770 --> 06:18.610
the index.

78
06:18.610 --> 06:23.470
And now when I print it, I'm able to get hold of that item D.

79
06:23.470 --> 06:31.360
Now this is quite confusing at first because there's a lot of nesting, and it can be a little bit difficult

80
06:31.360 --> 06:32.980
to wrap your head around it,

81
06:32.980 --> 06:40.690
but if you try it a few times with some of the other items in the nested list inside a dictionary, or

82
06:40.690 --> 06:48.100
the nested list inside a list, then eventually it will make sense and it's not actually so complicated.

83
06:48.460 --> 06:55.120
Now, other things that you can nest is you can actually nest a dictionary within a dictionary itself.

84
06:55.150 --> 07:03.660
So if I have this travel_log and I'm going to copy it, and then comment it out so that I can have a different

85
07:03.660 --> 07:12.480
version of it. Now if I wanted to, instead of storing a list as the value of the key,

86
07:12.510 --> 07:16.410
France, I can also store a dictionary.

87
07:16.410 --> 07:25.100
So now this is a dictionary which has a key of France, and the value for that key happens to be another

88
07:25.100 --> 07:25.910
dictionary.

89
07:25.910 --> 07:28.340
So what's going to go inside this dictionary?

90
07:28.340 --> 07:33.500
Well, if I was really building a travel_log, I might want to log.

91
07:33.500 --> 07:36.230
Well, how many times have I visited this country.

92
07:36.230 --> 07:46.940
So let's create a "num_times_visited" key for this France and make sure that we've got it as a string.

93
07:46.940 --> 07:49.640
And then for the value we could have a number.

94
07:49.640 --> 07:58.580
So let's say I've been to France 8 times, then that would be the first key / value pair in our France,

95
07:58.760 --> 08:00.260
entry.

96
08:00.260 --> 08:06.380
And then we've also got another key called cities_visited.

97
08:07.400 --> 08:16.570
And now what we can do is we can save the list into the value for that key.

98
08:16.600 --> 08:26.500
So now we've got a list nested inside a dictionary which is nested inside another dictionary.

99
08:26.500 --> 08:31.990
And if I was to complete my travel_log then it might look something like this.

100
08:31.990 --> 08:35.500
And you can see that our data is more complex.

101
08:35.500 --> 08:43.890
We've got more key/value pairs associated, and we've essentially got a much better way of organizing

102
08:43.890 --> 08:45.840
the data for our travel_log.

103
08:45.870 --> 08:52.260
So now comes the question of how would you access some of the items from this more complex travel_log?

104
08:52.260 --> 08:58.050
Can you figure out how to print out Stuttgart from this travel_log?

105
08:58.050 --> 09:04.860
Figure out how you might access this dictionary and then how you might access this list, and then how

106
09:04.860 --> 09:08.640
you might access this item in the list so that you can print it out.

107
09:08.670 --> 09:10.080
Pause the video now.

108
09:14.040 --> 09:14.520
All right.

109
09:14.520 --> 09:16.350
So let's go step by step.

110
09:16.350 --> 09:22.170
The first step going inside this dictionary is we have a key and we have a value.

111
09:22.170 --> 09:24.540
So let's try and pick out this value.

112
09:24.570 --> 09:32.060
We can do that by printing out the travel_log and then using square brackets to tap into Germany.

113
09:32.450 --> 09:40.610
Now when we print this line, we should be able to get this dictionary that contains all of this data.

114
09:40.610 --> 09:46.490
So to narrow it down to this list we have to again use the next key.

115
09:46.490 --> 09:53.590
So add a set of square brackets and then use cities_visited and making sure that you've actually typed

116
09:53.590 --> 09:55.660
it right without any typos.

117
09:55.690 --> 10:02.830
Often it's easier to just copy and paste, and now I should be able to get the list that's stored under

118
10:02.830 --> 10:03.730
that key.

119
10:03.760 --> 10:09.880
So finally, to access this item in the list, it's at index 012.

120
10:09.910 --> 10:12.310
So another set of square brackets,

121
10:12.310 --> 10:14.110
put the index 2 in there.

122
10:14.110 --> 10:17.230
And we've got Stuttgart being printed out here.

123
10:17.410 --> 10:18.910
So there you have it.

124
10:18.910 --> 10:25.390
You have lists inside dictionaries, dictionaries inside dictionaries, lists, inside lists, and a

125
10:25.390 --> 10:31.660
whole lot of different ways of organizing your data and working with dictionaries in Python.

126
10:31.810 --> 10:38.410
Next, we've got a quiz for you to test some of your knowledge on these dictionaries, and hopefully

127
10:38.410 --> 10:40.630
you will pass with flying colors.