Now that we've seen lists as well as dictionaries, I want to talk about a concept that you often see
in both of these collection types, and that's something called nesting.
Now, if we imagine a list or a dictionary being something like a folder where lots of things can be
stored inside it, then nesting lists and dictionaries is just a matter of putting one inside the other.
For example, here's a dictionary that is very simple.
It's only got one key / value pair.
Now we know that we can add multiple key value pairs into the same dictionary by just adding a bunch
of commas to separate them.
Now what if instead of having a simple value like a string or a number, I could also put a list as
a value as well.
Similarly, I could also use a dictionary as a value.
In this case, we've got a list as the value for key,
this first one, and we've got a dictionary as the value for key2.
Notice how this is a list and a dictionary nested inside another dictionary.
The structure gets a little bit more complex, but it gives us more flexibility when we're trying to
store more complex pieces of data.
Coming back to our code, let's go ahead and create some sample dictionaries to see what this actually
looks like in real life.
For example, this could be a dictionary that contains the country as the key and the city that is the
capital as the value.
So for France, that's Paris, for Germany, that's Berlin.
This is a very simple dictionary that you've seen already.
Now, if I wanted to nest a list in a dictionary, then I would be able to represent even more complex
data.
For example, if I had a travel_log where I was going to collect a dictionary of all the cities I had
been for each of the countries I've traveled to.
So, for example, if I had traveled to France and I wanted to say that I've been to multiple cities,
I can't simply just say, Paris, and then Lille, Dijon,
that doesn't really work, because each key can only have one value.
The only way that we can make these three pieces of data, one value, is by turning it into a list
like so.
So now in our travel_log dictionary, as represented by the curly braces, we have one key/value pair.
And it just so happens that the value in this case is a list.
And of course, you can go on and add as many entries as you would like,
and still preserving this kind of structure of key being a string and the value being a list.
Now I've got a challenge for you to test your understanding of how lists nested inside dictionaries
might work.
I want you to guess and figure out how can you print out the Lille string from this travel_log.
So think about how you might access items in a dictionary, and then think about how you access items
in a list.
And the goal is just to print out this Lille.
Pause the video.
Give this a go.
Okay, so let's first start with accessing this list.
So if we go ahead and use our print statement to tap into the travel_log, we know that we can access
the value of a dictionary by using the key.
So we use the square brackets.
And then we have to type the key which in this case is France.
So if we print this right now we're getting hold of the list.
And we know how to work with lists already.
So if we want the item at index zero one then we just need another set of square brackets and add one
in there.
So now if we run this you can see we're able to get hold of this item.
Now, we've previously seen that you can nest lists inside other lists, and you can end up with a many
layered nested list.
So for example, if I had a list that had the letters A and B, I can nest a list inside there at the
index[2],
So, 0 1 2.
The third item in this list is another list.
And I'm going to add C and D.
So now we have what's called a 2D list,
we have a list,
and then we have a list inside that list.
So here's another challenge for you.
See if you can figure out how to print out this letter D from the nested list.
Think about what we did before and think about what you've learned previously about nested lists,
to complete this challenge. Pause the video now.
Okay, so let's first access this list.
So if we go ahead and print our nested list and then we put in the index of this list, it would be
at 0, 1, 2.
So let's put two inside the set of square brackets.
I'm just going to comment this other print statement out.
And now you can see we've got hold of C and D.
So now that we've got that list how would we access D if we were working with this list.
Well we would tap into the item at index 1.
So what we know is this part of the code results in this list.
Then to get hold of D, all we need to do is add another set of square brackets and then add 1 as
the index.
And now when I print it, I'm able to get hold of that item D.
Now this is quite confusing at first because there's a lot of nesting, and it can be a little bit difficult
to wrap your head around it,
but if you try it a few times with some of the other items in the nested list inside a dictionary, or
the nested list inside a list, then eventually it will make sense and it's not actually so complicated.
Now, other things that you can nest is you can actually nest a dictionary within a dictionary itself.
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
version of it. Now if I wanted to, instead of storing a list as the value of the key,
France, I can also store a dictionary.
So now this is a dictionary which has a key of France, and the value for that key happens to be another
dictionary.
So what's going to go inside this dictionary?
Well, if I was really building a travel_log, I might want to log.
Well, how many times have I visited this country.
So let's create a "num_times_visited" key for this France and make sure that we've got it as a string.
And then for the value we could have a number.
So let's say I've been to France 8 times, then that would be the first key / value pair in our France,
entry.
And then we've also got another key called cities_visited.
And now what we can do is we can save the list into the value for that key.
So now we've got a list nested inside a dictionary which is nested inside another dictionary.
And if I was to complete my travel_log then it might look something like this.
And you can see that our data is more complex.
We've got more key/value pairs associated, and we've essentially got a much better way of organizing
the data for our travel_log.
So now comes the question of how would you access some of the items from this more complex travel_log?
Can you figure out how to print out Stuttgart from this travel_log?
Figure out how you might access this dictionary and then how you might access this list, and then how
you might access this item in the list so that you can print it out.
Pause the video now.
All right.
So let's go step by step.
The first step going inside this dictionary is we have a key and we have a value.
So let's try and pick out this value.
We can do that by printing out the travel_log and then using square brackets to tap into Germany.
Now when we print this line, we should be able to get this dictionary that contains all of this data.
So to narrow it down to this list we have to again use the next key.
So add a set of square brackets and then use cities_visited and making sure that you've actually typed
it right without any typos.
Often it's easier to just copy and paste, and now I should be able to get the list that's stored under
that key.
So finally, to access this item in the list, it's at index 012.
So another set of square brackets,
put the index 2 in there.
And we've got Stuttgart being printed out here.
So there you have it.
You have lists inside dictionaries, dictionaries inside dictionaries, lists, inside lists, and a
whole lot of different ways of organizing your data and working with dictionaries in Python.
Next, we've got a quiz for you to test some of your knowledge on these dictionaries, and hopefully
you will pass with flying colors.