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,
this idea of nesting isn't limited dictionaries by the way.
You could also just nest a list in a list.
So you could have a list which is A
and then B, and then the third item just happens to be another list which has C and D.
This is perfectly valid Python code
but it's not quite as useful as nesting a list in a dictionary or a dictionary
in a dictionary, because the way that data is structured. Now,
what if you wanted to nest a dictionary in a dictionary?
Let's say that we wanted to expand our travel log.
And instead of just storing the cities that I visited in each country,
what if I wanted to also keep track of how many visits I've made to that
country?
Or what if I wanted to actually label what this piece of data is?
Because at the moment it's kind of like here's
a bunch of cities that's associated with the country,
but it doesn't really describe this list.
And I'd like to give this to you as a challenge,
have a go at changing the France entry so that it contains another dictionary.
For the nested dictionary use the key cities_visited,
and for the value you can keep the list of Paris, Lille and Dijon.
Pause the video now and see if you can get that to work.
Yeah.
All right, so here's the solution. By nesting a dictionary
we can label the Paris, Lille and Dijon lists as cities visited.
All I need to do is create the string,
"cities_visited" and add a colon after it. But there's one more step.
We have to add a pair of curly braces so that France is associated with a single
value, namely our nested dictionary.
Now that we have a dictionary of the travel log,
all the countries I've been to, and each country has a value that is a dictionary
in itself and it can store multiple pieces of data
including the cities I've visited in that country
as well as things such as the total number of visits,
which in this case would have a string as the key and a
number of as the value. Now
have a go at creating your own travel log or modifying the second part of the
travel log so that you also have a dictionary nested in a dictionary and you're
able to represent something like this,
the number of cities you visited in a list and another additional piece of data,
if you wanted to.
Right,
And hopefully would have done something similar to this.
And so now our data structure is such that we've got a list nested inside a
dictionary, which is in itself nested in another dictionary.
Now that we've seen nesting lists inside dictionaries, dictionaries inside
dictionaries,
the last thing I want to show you is nesting a dictionary inside a list.
So we could basically have multiple dictionaries inside a single list.
So remember that lists are ordered and they're accessed by the positions inside the
list. So this dictionary would be the item at index zero,
this one at index one and so on and so forth.
Whereas inside a dictionary,
the items are accessed by their keys like this France or Germany.
Now I want to do the last type which is nesting a dictionary
in a list.
And I'm going to be working with the same travel log that we had previously,
but I want to change it so that instead of having this being one giant
dictionary with key-value pairs,
I want to change it so that each of these entries are a dictionary in itself.
Instead of having France be the key of this dictionary here,
I want the turn it into its own key-value pair. Going inside this dictionary,
I'm going to create a new key
which is called country and then put France as the value for that key.
So now I have an entire dictionary that has three pieces of data.
The country visited, the cities visited,
and the total number of visits. Let's do the same for Germany as well.
So now you can see we've got two dictionaries and instead of holding these two
dictionaries inside a dictionary,
I'm going to change it so that it's now a list. I'm going to turn these curly
braces into square brackets
and this way we can add as many of these dictionaries inside the list and
iterate through them if we need to.
When we've been creating dictionaries that only have one or two key-value pairs,
we've kind of just kept it all on one line.
But once you've got more and more key-value pairs in the dictionary,
it's usually a good idea to go ahead and separate out each of the entries,
Right?
Like what I'm doing here.
Now it's a little bit easier to see that we have a list which contains two
items, and each item is a dictionary.
Each dictionary has three key-value pairs and they all contain different types
of data. The first one has a value which is a string,
the second one holds a list and the third one holds a number.
The data types inside a dictionary can be completely mixed up if you want it to.
But what can't change is you still need a key and a value separated by a colon.
Now the only downside to this is you want to make sure that you're actually
aware that later on when you try to pull out the number of visits,
it's actually a number that you're working with rather than a string or when
you're trying to get hold of the cities visited
it's a list rather than a dictionary, for example.
So now that we've the various different types of nesting,
it's time for you to actually really understand this and get to grips with this
concept by completing a challenge yourself. In the next lesson,
I've got a coding challenge for you based on this travel log and you'll need to
use what you've learned so far to be able to complete it.
So for all of that and more, I'll see you on the next lesson.